wd.c revision 1.239 1 /* $NetBSD: wd.c,v 1.239 2003/03/21 22:40:56 thorpej 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.239 2003/03/21 22:40:56 thorpej 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
292 wd->atabus = adev->adev_bustype;
293 wd->openings = adev->adev_openings;
294 wd->drvp = adev->adev_drv_data;
295 wd->wdc_softc = parent;
296 /* give back our softc to our caller */
297 wd->drvp->drv_softc = &wd->sc_dev;
298
299 /* read our drive info */
300 if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
301 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
302 return;
303 }
304
305 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
306 i < sizeof(wd->sc_params.atap_model); i++) {
307 c = *p++;
308 if (c == '\0')
309 break;
310 if (c != ' ') {
311 if (blank) {
312 *q++ = ' ';
313 blank = 0;
314 }
315 *q++ = c;
316 } else
317 blank = 1;
318 }
319 *q++ = '\0';
320
321 printf(": <%s>\n", buf);
322
323 wdq = wd_lookup_quirks(buf);
324 if (wdq != NULL)
325 wd->sc_quirks = wdq->wdq_quirks;
326
327 if ((wd->sc_params.atap_multi & 0xff) > 1) {
328 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
329 } else {
330 wd->sc_multi = 1;
331 }
332
333 printf("%s: drive supports %d-sector PIO transfers,",
334 wd->sc_dev.dv_xname, wd->sc_multi);
335
336 /* 48-bit LBA addressing */
337 if ((wd->sc_params.atap_cmd2_en & WDC_CAP_LBA48) != 0)
338 wd->sc_flags |= WDF_LBA48;
339
340 /* Prior to ATA-4, LBA was optional. */
341 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
342 wd->sc_flags |= WDF_LBA;
343 #if 0
344 /* ATA-4 requires LBA. */
345 if (wd->sc_params.atap_ataversion != 0xffff &&
346 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
347 wd->sc_flags |= WDF_LBA;
348 #endif
349
350 if ((wd->sc_flags & WDF_LBA48) != 0) {
351 printf(" LBA48 addressing\n");
352 wd->sc_capacity =
353 ((u_int64_t) wd->sc_params.__reserved6[11] << 48) |
354 ((u_int64_t) wd->sc_params.__reserved6[10] << 32) |
355 ((u_int64_t) wd->sc_params.__reserved6[9] << 16) |
356 ((u_int64_t) wd->sc_params.__reserved6[8] << 0);
357 } else if ((wd->sc_flags & WDF_LBA) != 0) {
358 printf(" LBA addressing\n");
359 wd->sc_capacity =
360 (wd->sc_params.atap_capacity[1] << 16) |
361 wd->sc_params.atap_capacity[0];
362 } else {
363 printf(" chs addressing\n");
364 wd->sc_capacity =
365 wd->sc_params.atap_cylinders *
366 wd->sc_params.atap_heads *
367 wd->sc_params.atap_sectors;
368 }
369 format_bytes(pbuf, sizeof(pbuf),
370 (u_int64_t)wd->sc_capacity * DEV_BSIZE);
371 printf("%s: %s, %d cyl, %d head, %d sec, "
372 "%d bytes/sect x %d 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, 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 /* locate the major number */
428 bmaj = bdevsw_lookup_major(&wd_bdevsw);
429 cmaj = cdevsw_lookup_major(&wd_cdevsw);
430
431 s = splbio();
432
433 /* Kill off any queued buffers. */
434 while ((bp = BUFQ_GET(&sc->sc_q)) != NULL) {
435 bp->b_error = EIO;
436 bp->b_flags |= B_ERROR;
437 bp->b_resid = bp->b_bcount;
438 biodone(bp);
439 }
440
441 bufq_free(&sc->sc_q);
442
443 splx(s);
444
445 /* Nuke the vnodes for any open instances. */
446 for (i = 0; i < MAXPARTITIONS; i++) {
447 mn = WDMINOR(self->dv_unit, i);
448 vdevgone(bmaj, mn, mn, VBLK);
449 vdevgone(cmaj, mn, mn, VCHR);
450 }
451
452 /* Detach disk. */
453 disk_detach(&sc->sc_dk);
454
455 /* Get rid of the shutdown hook. */
456 if (sc->sc_sdhook != NULL)
457 shutdownhook_disestablish(sc->sc_sdhook);
458
459 #if NRND > 0
460 /* Unhook the entropy source. */
461 rnd_detach_source(&sc->rnd_source);
462 #endif
463
464 return (0);
465 }
466
467 /*
468 * Read/write routine for a buffer. Validates the arguments and schedules the
469 * transfer. Does not wait for the transfer to complete.
470 */
471 void
472 wdstrategy(bp)
473 struct buf *bp;
474 {
475 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(bp->b_dev));
476 struct disklabel *lp = wd->sc_dk.dk_label;
477 daddr_t blkno;
478 int s;
479
480 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
481 DEBUG_XFERS);
482
483 /* Valid request? */
484 if (bp->b_blkno < 0 ||
485 (bp->b_bcount % lp->d_secsize) != 0 ||
486 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
487 bp->b_error = EINVAL;
488 goto bad;
489 }
490
491 /* If device invalidated (e.g. media change, door open), error. */
492 if ((wd->sc_flags & WDF_LOADED) == 0) {
493 bp->b_error = EIO;
494 goto bad;
495 }
496
497 /* If it's a null transfer, return immediately. */
498 if (bp->b_bcount == 0)
499 goto done;
500
501 /*
502 * Do bounds checking, adjust transfer. if error, process.
503 * If end of partition, just return.
504 */
505 if (bounds_check_with_label(bp, wd->sc_dk.dk_label,
506 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
507 goto done;
508
509 /*
510 * Now convert the block number to absolute and put it in
511 * terms of the device's logical block size.
512 */
513 if (lp->d_secsize >= DEV_BSIZE)
514 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
515 else
516 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
517
518 if (WDPART(bp->b_dev) != RAW_PART)
519 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
520
521 bp->b_rawblkno = blkno;
522
523 /* Queue transfer on drive, activate drive and controller if idle. */
524 s = splbio();
525 BUFQ_PUT(&wd->sc_q, bp);
526 wdstart(wd);
527 splx(s);
528 return;
529 bad:
530 bp->b_flags |= B_ERROR;
531 done:
532 /* Toss transfer; we're done early. */
533 bp->b_resid = bp->b_bcount;
534 biodone(bp);
535 }
536
537 /*
538 * Queue a drive for I/O.
539 */
540 void
541 wdstart(arg)
542 void *arg;
543 {
544 struct wd_softc *wd = arg;
545 struct buf *bp = NULL;
546
547 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
548 DEBUG_XFERS);
549 while (wd->openings > 0) {
550
551 /* Is there a buf for us ? */
552 if ((bp = BUFQ_GET(&wd->sc_q)) == NULL)
553 return;
554
555 /*
556 * Make the command. First lock the device
557 */
558 wd->openings--;
559
560 wd->retries = 0;
561 __wdstart(wd, bp);
562 }
563 }
564
565 static void
566 wd_split_mod15_write(struct buf *bp)
567 {
568 struct buf *obp = bp->b_private;
569 struct wd_softc *sc = wd_cd.cd_devs[DISKUNIT(obp->b_dev)];
570
571 if (__predict_false(bp->b_flags & B_ERROR) != 0) {
572 /*
573 * Propagate the error. If this was the first half of
574 * the original transfer, make sure to account for that
575 * in the residual.
576 */
577 if (bp->b_data == obp->b_data)
578 bp->b_resid += bp->b_bcount;
579 goto done;
580 }
581
582 /*
583 * If this was the second half of the transfer, we're all done!
584 */
585 if (bp->b_data != obp->b_data)
586 goto done;
587
588 /*
589 * Advance the pointer to the second half and issue that command
590 * using the same opening.
591 */
592 bp->b_flags = obp->b_flags | B_CALL;
593 bp->b_data += bp->b_bcount;
594 bp->b_blkno += (bp->b_bcount / 512);
595 bp->b_rawblkno += (bp->b_bcount / 512);
596 __wdstart(sc, bp);
597 return;
598
599 done:
600 obp->b_flags |= (bp->b_flags & (B_EINTR|B_ERROR));
601 obp->b_error = bp->b_error;
602 obp->b_resid = bp->b_resid;
603 pool_put(&bufpool, bp);
604 biodone(obp);
605 sc->openings++;
606 /* wddone() will call wdstart() */
607 }
608
609 void
610 __wdstart(wd, bp)
611 struct wd_softc *wd;
612 struct buf *bp;
613 {
614
615 /*
616 * Deal with the "split mod15 write" quirk. We just divide the
617 * transfer in two, doing the first half and then then second half
618 * with the same command opening.
619 *
620 * Note we MUST do this here, because we can't let insertion
621 * into the bufq cause the transfers to be re-merged.
622 */
623 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
624 (bp->b_flags & B_READ) == 0 &&
625 bp->b_bcount > 512 &&
626 ((bp->b_bcount / 512) % 15) == 1)) {
627 struct buf *nbp;
628
629 /* already at splbio */
630 nbp = pool_get(&bufpool, PR_NOWAIT);
631 if (__predict_false(nbp == NULL)) {
632 /* No memory -- fail the iop. */
633 bp->b_error = ENOMEM;
634 bp->b_flags |= B_ERROR;
635 bp->b_resid = bp->b_bcount;
636 biodone(bp);
637 wd->openings++;
638 return;
639 }
640
641 BUF_INIT(nbp);
642 nbp->b_error = 0;
643 nbp->b_proc = bp->b_proc;
644 nbp->b_vp = NULLVP;
645 nbp->b_dev = bp->b_dev;
646
647 nbp->b_bcount = bp->b_bcount / 2;
648 nbp->b_bufsize = bp->b_bcount / 2;
649 nbp->b_data = bp->b_data;
650
651 nbp->b_blkno = bp->b_blkno;
652 nbp->b_rawblkno = bp->b_rawblkno;
653
654 nbp->b_flags = bp->b_flags | B_CALL;
655 nbp->b_iodone = wd_split_mod15_write;
656
657 /* Put ptr to orig buf in b_private and use new buf */
658 nbp->b_private = bp;
659 bp = nbp;
660 }
661
662 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
663 wd->sc_wdc_bio.blkdone =0;
664 wd->sc_bp = bp;
665 /*
666 * If we're retrying, retry in single-sector mode. This will give us
667 * the sector number of the problem, and will eventually allow the
668 * transfer to succeed.
669 */
670 if (wd->sc_multi == 1 || wd->retries >= WDIORETRIES_SINGLE)
671 wd->sc_wdc_bio.flags = ATA_SINGLE;
672 else
673 wd->sc_wdc_bio.flags = 0;
674 if (wd->sc_flags & WDF_LBA48)
675 wd->sc_wdc_bio.flags |= ATA_LBA48;
676 if (wd->sc_flags & WDF_LBA)
677 wd->sc_wdc_bio.flags |= ATA_LBA;
678 if (bp->b_flags & B_READ)
679 wd->sc_wdc_bio.flags |= ATA_READ;
680 wd->sc_wdc_bio.bcount = bp->b_bcount;
681 wd->sc_wdc_bio.databuf = bp->b_data;
682 /* Instrumentation. */
683 disk_busy(&wd->sc_dk);
684 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
685 case WDC_TRY_AGAIN:
686 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
687 break;
688 case WDC_QUEUED:
689 case WDC_COMPLETE:
690 break;
691 default:
692 panic("__wdstart: bad return code from ata_bio()");
693 }
694 }
695
696 void
697 wddone(v)
698 void *v;
699 {
700 struct wd_softc *wd = v;
701 struct buf *bp = wd->sc_bp;
702 const char *errmsg;
703 int do_perror = 0;
704 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
705 DEBUG_XFERS);
706
707 if (bp == NULL)
708 return;
709 bp->b_resid = wd->sc_wdc_bio.bcount;
710 switch (wd->sc_wdc_bio.error) {
711 case ERR_DMA:
712 errmsg = "DMA error";
713 goto retry;
714 case ERR_DF:
715 errmsg = "device fault";
716 goto retry;
717 case TIMEOUT:
718 errmsg = "device timeout";
719 goto retry;
720 case ERROR:
721 /* Don't care about media change bits */
722 if (wd->sc_wdc_bio.r_error != 0 &&
723 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
724 goto noerror;
725 errmsg = "error";
726 do_perror = 1;
727 retry: /* Just reset and retry. Can we do more ? */
728 wd->atabus->ata_reset_channel(wd->drvp);
729 diskerr(bp, "wd", errmsg, LOG_PRINTF,
730 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
731 if (wd->retries < WDIORETRIES)
732 printf(", retrying\n");
733 if (do_perror)
734 wdperror(wd);
735 if (wd->retries < WDIORETRIES) {
736 wd->retries++;
737 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
738 wdrestart, wd);
739 return;
740 }
741 printf("\n");
742 bp->b_flags |= B_ERROR;
743 bp->b_error = EIO;
744 break;
745 case NOERROR:
746 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
747 printf("%s: soft error (corrected)\n",
748 wd->sc_dev.dv_xname);
749 break;
750 case ERR_NODEV:
751 bp->b_flags |= B_ERROR;
752 bp->b_error = EIO;
753 break;
754 }
755 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
756 (bp->b_flags & B_READ));
757 #if NRND > 0
758 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
759 #endif
760 /* XXX Yuck, but we don't want to increment openings in this case */
761 if (__predict_false((bp->b_flags & B_CALL) != 0 &&
762 bp->b_iodone == wd_split_mod15_write))
763 biodone(bp);
764 else {
765 biodone(bp);
766 wd->openings++;
767 }
768 wdstart(wd);
769 }
770
771 void
772 wdrestart(v)
773 void *v;
774 {
775 struct wd_softc *wd = v;
776 struct buf *bp = wd->sc_bp;
777 int s;
778 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
779 DEBUG_XFERS);
780
781 s = splbio();
782 __wdstart(v, bp);
783 splx(s);
784 }
785
786 int
787 wdread(dev, uio, flags)
788 dev_t dev;
789 struct uio *uio;
790 int flags;
791 {
792
793 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
794 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
795 }
796
797 int
798 wdwrite(dev, uio, flags)
799 dev_t dev;
800 struct uio *uio;
801 int flags;
802 {
803
804 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
805 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
806 }
807
808 /*
809 * Wait interruptibly for an exclusive lock.
810 *
811 * XXX
812 * Several drivers do this; it should be abstracted and made MP-safe.
813 */
814 int
815 wdlock(wd)
816 struct wd_softc *wd;
817 {
818 int error;
819 int s;
820
821 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
822
823 s = splbio();
824
825 while ((wd->sc_flags & WDF_LOCKED) != 0) {
826 wd->sc_flags |= WDF_WANTED;
827 if ((error = tsleep(wd, PRIBIO | PCATCH,
828 "wdlck", 0)) != 0) {
829 splx(s);
830 return error;
831 }
832 }
833 wd->sc_flags |= WDF_LOCKED;
834 splx(s);
835 return 0;
836 }
837
838 /*
839 * Unlock and wake up any waiters.
840 */
841 void
842 wdunlock(wd)
843 struct wd_softc *wd;
844 {
845
846 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
847
848 wd->sc_flags &= ~WDF_LOCKED;
849 if ((wd->sc_flags & WDF_WANTED) != 0) {
850 wd->sc_flags &= ~WDF_WANTED;
851 wakeup(wd);
852 }
853 }
854
855 int
856 wdopen(dev, flag, fmt, p)
857 dev_t dev;
858 int flag, fmt;
859 struct proc *p;
860 {
861 struct wd_softc *wd;
862 int part, error;
863
864 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
865 wd = device_lookup(&wd_cd, WDUNIT(dev));
866 if (wd == NULL)
867 return (ENXIO);
868
869 /*
870 * If this is the first open of this device, add a reference
871 * to the adapter.
872 */
873 if (wd->sc_dk.dk_openmask == 0 &&
874 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
875 return (error);
876
877 if ((error = wdlock(wd)) != 0)
878 goto bad4;
879
880 if (wd->sc_dk.dk_openmask != 0) {
881 /*
882 * If any partition is open, but the disk has been invalidated,
883 * disallow further opens.
884 */
885 if ((wd->sc_flags & WDF_LOADED) == 0) {
886 error = EIO;
887 goto bad3;
888 }
889 } else {
890 if ((wd->sc_flags & WDF_LOADED) == 0) {
891 wd->sc_flags |= WDF_LOADED;
892
893 /* Load the physical device parameters. */
894 wd_get_params(wd, AT_WAIT, &wd->sc_params);
895
896 /* Load the partition info if not already loaded. */
897 wdgetdisklabel(wd);
898 }
899 }
900
901 part = WDPART(dev);
902
903 /* Check that the partition exists. */
904 if (part != RAW_PART &&
905 (part >= wd->sc_dk.dk_label->d_npartitions ||
906 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
907 error = ENXIO;
908 goto bad;
909 }
910
911 /* Insure only one open at a time. */
912 switch (fmt) {
913 case S_IFCHR:
914 wd->sc_dk.dk_copenmask |= (1 << part);
915 break;
916 case S_IFBLK:
917 wd->sc_dk.dk_bopenmask |= (1 << part);
918 break;
919 }
920 wd->sc_dk.dk_openmask =
921 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
922
923 wdunlock(wd);
924 return 0;
925
926 bad:
927 if (wd->sc_dk.dk_openmask == 0) {
928 }
929
930 bad3:
931 wdunlock(wd);
932 bad4:
933 if (wd->sc_dk.dk_openmask == 0)
934 wd->atabus->ata_delref(wd->drvp);
935 return error;
936 }
937
938 int
939 wdclose(dev, flag, fmt, p)
940 dev_t dev;
941 int flag, fmt;
942 struct proc *p;
943 {
944 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
945 int part = WDPART(dev);
946 int error;
947
948 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
949 if ((error = wdlock(wd)) != 0)
950 return error;
951
952 switch (fmt) {
953 case S_IFCHR:
954 wd->sc_dk.dk_copenmask &= ~(1 << part);
955 break;
956 case S_IFBLK:
957 wd->sc_dk.dk_bopenmask &= ~(1 << part);
958 break;
959 }
960 wd->sc_dk.dk_openmask =
961 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
962
963 if (wd->sc_dk.dk_openmask == 0) {
964 wd_flushcache(wd, AT_WAIT);
965 /* XXXX Must wait for I/O to complete! */
966
967 if (! (wd->sc_flags & WDF_KLABEL))
968 wd->sc_flags &= ~WDF_LOADED;
969
970 wd->atabus->ata_delref(wd->drvp);
971 }
972
973 wdunlock(wd);
974 return 0;
975 }
976
977 void
978 wdgetdefaultlabel(wd, lp)
979 struct wd_softc *wd;
980 struct disklabel *lp;
981 {
982
983 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
984 memset(lp, 0, sizeof(struct disklabel));
985
986 lp->d_secsize = DEV_BSIZE;
987 lp->d_ntracks = wd->sc_params.atap_heads;
988 lp->d_nsectors = wd->sc_params.atap_sectors;
989 lp->d_ncylinders = wd->sc_params.atap_cylinders;
990 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
991
992 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
993 lp->d_type = DTYPE_ST506;
994 else
995 lp->d_type = DTYPE_ESDI;
996
997 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
998 strncpy(lp->d_packname, "fictitious", 16);
999 lp->d_secperunit = wd->sc_capacity;
1000 lp->d_rpm = 3600;
1001 lp->d_interleave = 1;
1002 lp->d_flags = 0;
1003
1004 lp->d_partitions[RAW_PART].p_offset = 0;
1005 lp->d_partitions[RAW_PART].p_size =
1006 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1007 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1008 lp->d_npartitions = RAW_PART + 1;
1009
1010 lp->d_magic = DISKMAGIC;
1011 lp->d_magic2 = DISKMAGIC;
1012 lp->d_checksum = dkcksum(lp);
1013 }
1014
1015 /*
1016 * Fabricate a default disk label, and try to read the correct one.
1017 */
1018 void
1019 wdgetdisklabel(wd)
1020 struct wd_softc *wd;
1021 {
1022 struct disklabel *lp = wd->sc_dk.dk_label;
1023 char *errstring;
1024
1025 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1026
1027 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1028
1029 wdgetdefaultlabel(wd, lp);
1030
1031 wd->sc_badsect[0] = -1;
1032
1033 if (wd->drvp->state > RECAL)
1034 wd->drvp->drive_flags |= DRIVE_RESET;
1035 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
1036 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1037 if (errstring) {
1038 /*
1039 * This probably happened because the drive's default
1040 * geometry doesn't match the DOS geometry. We
1041 * assume the DOS geometry is now in the label and try
1042 * again. XXX This is a kluge.
1043 */
1044 if (wd->drvp->state > RECAL)
1045 wd->drvp->drive_flags |= DRIVE_RESET;
1046 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
1047 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1048 }
1049 if (errstring) {
1050 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1051 return;
1052 }
1053
1054 if (wd->drvp->state > RECAL)
1055 wd->drvp->drive_flags |= DRIVE_RESET;
1056 #ifdef HAS_BAD144_HANDLING
1057 if ((lp->d_flags & D_BADSECT) != 0)
1058 bad144intern(wd);
1059 #endif
1060 }
1061
1062 void
1063 wdperror(wd)
1064 const struct wd_softc *wd;
1065 {
1066 static const char *const errstr0_3[] = {"address mark not found",
1067 "track 0 not found", "aborted command", "media change requested",
1068 "id not found", "media changed", "uncorrectable data error",
1069 "bad block detected"};
1070 static const char *const errstr4_5[] = {
1071 "obsolete (address mark not found)",
1072 "no media/write protected", "aborted command",
1073 "media change requested", "id not found", "media changed",
1074 "uncorrectable data error", "interface CRC error"};
1075 const char *const *errstr;
1076 int i;
1077 char *sep = "";
1078
1079 const char *devname = wd->sc_dev.dv_xname;
1080 struct ata_drive_datas *drvp = wd->drvp;
1081 int errno = wd->sc_wdc_bio.r_error;
1082
1083 if (drvp->ata_vers >= 4)
1084 errstr = errstr4_5;
1085 else
1086 errstr = errstr0_3;
1087
1088 printf("%s: (", devname);
1089
1090 if (errno == 0)
1091 printf("error not notified");
1092
1093 for (i = 0; i < 8; i++) {
1094 if (errno & (1 << i)) {
1095 printf("%s%s", sep, errstr[i]);
1096 sep = ", ";
1097 }
1098 }
1099 printf(")\n");
1100 }
1101
1102 int
1103 wdioctl(dev, xfer, addr, flag, p)
1104 dev_t dev;
1105 u_long xfer;
1106 caddr_t addr;
1107 int flag;
1108 struct proc *p;
1109 {
1110 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
1111 int error = 0;
1112 #ifdef __HAVE_OLD_DISKLABEL
1113 struct disklabel *newlabel = NULL;
1114 #endif
1115
1116 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1117
1118 if ((wd->sc_flags & WDF_LOADED) == 0)
1119 return EIO;
1120
1121 switch (xfer) {
1122 #ifdef HAS_BAD144_HANDLING
1123 case DIOCSBAD:
1124 if ((flag & FWRITE) == 0)
1125 return EBADF;
1126 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1127 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1128 bad144intern(wd);
1129 return 0;
1130 #endif
1131
1132 case DIOCGDINFO:
1133 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1134 return 0;
1135 #ifdef __HAVE_OLD_DISKLABEL
1136 case ODIOCGDINFO:
1137 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1138 if (newlabel == NULL)
1139 return EIO;
1140 *newlabel = *(wd->sc_dk.dk_label);
1141 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1142 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1143 else
1144 error = ENOTTY;
1145 free(newlabel, M_TEMP);
1146 return error;
1147 #endif
1148
1149 case DIOCGPART:
1150 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1151 ((struct partinfo *)addr)->part =
1152 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1153 return 0;
1154
1155 case DIOCWDINFO:
1156 case DIOCSDINFO:
1157 #ifdef __HAVE_OLD_DISKLABEL
1158 case ODIOCWDINFO:
1159 case ODIOCSDINFO:
1160 #endif
1161 {
1162 struct disklabel *lp;
1163
1164 if ((flag & FWRITE) == 0)
1165 return EBADF;
1166
1167 #ifdef __HAVE_OLD_DISKLABEL
1168 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1169 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1170 if (newlabel == NULL)
1171 return EIO;
1172 memset(newlabel, 0, sizeof newlabel);
1173 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1174 lp = newlabel;
1175 } else
1176 #endif
1177 lp = (struct disklabel *)addr;
1178
1179 if ((error = wdlock(wd)) != 0)
1180 goto bad;
1181 wd->sc_flags |= WDF_LABELLING;
1182
1183 error = setdisklabel(wd->sc_dk.dk_label,
1184 lp, /*wd->sc_dk.dk_openmask : */0,
1185 wd->sc_dk.dk_cpulabel);
1186 if (error == 0) {
1187 if (wd->drvp->state > RECAL)
1188 wd->drvp->drive_flags |= DRIVE_RESET;
1189 if (xfer == DIOCWDINFO
1190 #ifdef __HAVE_OLD_DISKLABEL
1191 || xfer == ODIOCWDINFO
1192 #endif
1193 )
1194 error = writedisklabel(WDLABELDEV(dev),
1195 wdstrategy, wd->sc_dk.dk_label,
1196 wd->sc_dk.dk_cpulabel);
1197 }
1198
1199 wd->sc_flags &= ~WDF_LABELLING;
1200 wdunlock(wd);
1201 bad:
1202 #ifdef __HAVE_OLD_DISKLABEL
1203 if (newlabel != NULL)
1204 free(newlabel, M_TEMP);
1205 #endif
1206 return error;
1207 }
1208
1209 case DIOCKLABEL:
1210 if (*(int *)addr)
1211 wd->sc_flags |= WDF_KLABEL;
1212 else
1213 wd->sc_flags &= ~WDF_KLABEL;
1214 return 0;
1215
1216 case DIOCWLABEL:
1217 if ((flag & FWRITE) == 0)
1218 return EBADF;
1219 if (*(int *)addr)
1220 wd->sc_flags |= WDF_WLABEL;
1221 else
1222 wd->sc_flags &= ~WDF_WLABEL;
1223 return 0;
1224
1225 case DIOCGDEFLABEL:
1226 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1227 return 0;
1228 #ifdef __HAVE_OLD_DISKLABEL
1229 case ODIOCGDEFLABEL:
1230 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1231 if (newlabel == NULL)
1232 return EIO;
1233 wdgetdefaultlabel(wd, newlabel);
1234 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1235 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1236 else
1237 error = ENOTTY;
1238 free(newlabel, M_TEMP);
1239 return error;
1240 #endif
1241
1242 #ifdef notyet
1243 case DIOCWFORMAT:
1244 if ((flag & FWRITE) == 0)
1245 return EBADF;
1246 {
1247 register struct format_op *fop;
1248 struct iovec aiov;
1249 struct uio auio;
1250
1251 fop = (struct format_op *)addr;
1252 aiov.iov_base = fop->df_buf;
1253 aiov.iov_len = fop->df_count;
1254 auio.uio_iov = &aiov;
1255 auio.uio_iovcnt = 1;
1256 auio.uio_resid = fop->df_count;
1257 auio.uio_segflg = 0;
1258 auio.uio_offset =
1259 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1260 auio.uio_procp = p;
1261 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1262 &auio);
1263 fop->df_count -= auio.uio_resid;
1264 fop->df_reg[0] = wdc->sc_status;
1265 fop->df_reg[1] = wdc->sc_error;
1266 return error;
1267 }
1268 #endif
1269
1270 case ATAIOCCOMMAND:
1271 /*
1272 * Make sure this command is (relatively) safe first
1273 */
1274 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1275 (flag & FWRITE) == 0)
1276 return (EBADF);
1277 {
1278 struct wd_ioctl *wi;
1279 atareq_t *atareq = (atareq_t *) addr;
1280 int error;
1281
1282 wi = wi_get();
1283 wi->wi_softc = wd;
1284 wi->wi_atareq = *atareq;
1285
1286 if (atareq->datalen && atareq->flags &
1287 (ATACMD_READ | ATACMD_WRITE)) {
1288 wi->wi_iov.iov_base = atareq->databuf;
1289 wi->wi_iov.iov_len = atareq->datalen;
1290 wi->wi_uio.uio_iov = &wi->wi_iov;
1291 wi->wi_uio.uio_iovcnt = 1;
1292 wi->wi_uio.uio_resid = atareq->datalen;
1293 wi->wi_uio.uio_offset = 0;
1294 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1295 wi->wi_uio.uio_rw =
1296 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1297 wi->wi_uio.uio_procp = p;
1298 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1299 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1300 minphys, &wi->wi_uio);
1301 } else {
1302 /* No need to call physio if we don't have any
1303 user data */
1304 wi->wi_bp.b_flags = 0;
1305 wi->wi_bp.b_data = 0;
1306 wi->wi_bp.b_bcount = 0;
1307 wi->wi_bp.b_dev = 0;
1308 wi->wi_bp.b_proc = p;
1309 wdioctlstrategy(&wi->wi_bp);
1310 error = wi->wi_bp.b_error;
1311 }
1312 *atareq = wi->wi_atareq;
1313 wi_free(wi);
1314 return(error);
1315 }
1316
1317 default:
1318 return ENOTTY;
1319 }
1320
1321 #ifdef DIAGNOSTIC
1322 panic("wdioctl: impossible");
1323 #endif
1324 }
1325
1326 #ifdef B_FORMAT
1327 int
1328 wdformat(struct buf *bp)
1329 {
1330
1331 bp->b_flags |= B_FORMAT;
1332 return wdstrategy(bp);
1333 }
1334 #endif
1335
1336 int
1337 wdsize(dev)
1338 dev_t dev;
1339 {
1340 struct wd_softc *wd;
1341 int part, omask;
1342 int size;
1343
1344 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1345
1346 wd = device_lookup(&wd_cd, WDUNIT(dev));
1347 if (wd == NULL)
1348 return (-1);
1349
1350 part = WDPART(dev);
1351 omask = wd->sc_dk.dk_openmask & (1 << part);
1352
1353 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1354 return (-1);
1355 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1356 size = -1;
1357 else
1358 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1359 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1360 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1361 return (-1);
1362 return (size);
1363 }
1364
1365 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1366 static int wddoingadump = 0;
1367 static int wddumprecalibrated = 0;
1368 static int wddumpmulti = 1;
1369
1370 /*
1371 * Dump core after a system crash.
1372 */
1373 int
1374 wddump(dev, blkno, va, size)
1375 dev_t dev;
1376 daddr_t blkno;
1377 caddr_t va;
1378 size_t size;
1379 {
1380 struct wd_softc *wd; /* disk unit to do the I/O */
1381 struct disklabel *lp; /* disk's disklabel */
1382 int part, err;
1383 int nblks; /* total number of sectors left to write */
1384
1385 /* Check if recursive dump; if so, punt. */
1386 if (wddoingadump)
1387 return EFAULT;
1388 wddoingadump = 1;
1389
1390 wd = device_lookup(&wd_cd, WDUNIT(dev));
1391 if (wd == NULL)
1392 return (ENXIO);
1393
1394 part = WDPART(dev);
1395
1396 /* Convert to disk sectors. Request must be a multiple of size. */
1397 lp = wd->sc_dk.dk_label;
1398 if ((size % lp->d_secsize) != 0)
1399 return EFAULT;
1400 nblks = size / lp->d_secsize;
1401 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1402
1403 /* Check transfer bounds against partition size. */
1404 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1405 return EINVAL;
1406
1407 /* Offset block number to start of partition. */
1408 blkno += lp->d_partitions[part].p_offset;
1409
1410 /* Recalibrate, if first dump transfer. */
1411 if (wddumprecalibrated == 0) {
1412 wddumpmulti = wd->sc_multi;
1413 wddumprecalibrated = 1;
1414 wd->drvp->state = RESET;
1415 }
1416
1417 while (nblks > 0) {
1418 again:
1419 wd->sc_bp = NULL;
1420 wd->sc_wdc_bio.blkno = blkno;
1421 wd->sc_wdc_bio.flags = ATA_POLL;
1422 if (wddumpmulti == 1)
1423 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1424 if (wd->sc_flags & WDF_LBA48)
1425 wd->sc_wdc_bio.flags |= ATA_LBA48;
1426 if (wd->sc_flags & WDF_LBA)
1427 wd->sc_wdc_bio.flags |= ATA_LBA;
1428 wd->sc_wdc_bio.bcount =
1429 min(nblks, wddumpmulti) * lp->d_secsize;
1430 wd->sc_wdc_bio.databuf = va;
1431 #ifndef WD_DUMP_NOT_TRUSTED
1432 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1433 case WDC_TRY_AGAIN:
1434 panic("wddump: try again");
1435 break;
1436 case WDC_QUEUED:
1437 panic("wddump: polled command has been queued");
1438 break;
1439 case WDC_COMPLETE:
1440 break;
1441 }
1442 switch(wd->sc_wdc_bio.error) {
1443 case TIMEOUT:
1444 printf("wddump: device timed out");
1445 err = EIO;
1446 break;
1447 case ERR_DF:
1448 printf("wddump: drive fault");
1449 err = EIO;
1450 break;
1451 case ERR_DMA:
1452 printf("wddump: DMA error");
1453 err = EIO;
1454 break;
1455 case ERROR:
1456 printf("wddump: ");
1457 wdperror(wd);
1458 err = EIO;
1459 break;
1460 case NOERROR:
1461 err = 0;
1462 break;
1463 default:
1464 panic("wddump: unknown error type");
1465 }
1466 if (err != 0) {
1467 if (wddumpmulti != 1) {
1468 wddumpmulti = 1; /* retry in single-sector */
1469 printf(", retrying\n");
1470 goto again;
1471 }
1472 printf("\n");
1473 return err;
1474 }
1475 #else /* WD_DUMP_NOT_TRUSTED */
1476 /* Let's just talk about this first... */
1477 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1478 unit, va, cylin, head, sector);
1479 delay(500 * 1000); /* half a second */
1480 #endif
1481
1482 /* update block count */
1483 nblks -= min(nblks, wddumpmulti);
1484 blkno += min(nblks, wddumpmulti);
1485 va += min(nblks, wddumpmulti) * lp->d_secsize;
1486 }
1487
1488 wddoingadump = 0;
1489 return 0;
1490 }
1491
1492 #ifdef HAS_BAD144_HANDLING
1493 /*
1494 * Internalize the bad sector table.
1495 */
1496 void
1497 bad144intern(wd)
1498 struct wd_softc *wd;
1499 {
1500 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1501 struct disklabel *lp = wd->sc_dk.dk_label;
1502 int i = 0;
1503
1504 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1505
1506 for (; i < NBT_BAD; i++) {
1507 if (bt->bt_bad[i].bt_cyl == 0xffff)
1508 break;
1509 wd->sc_badsect[i] =
1510 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1511 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1512 (bt->bt_bad[i].bt_trksec & 0xff);
1513 }
1514 for (; i < NBT_BAD+1; i++)
1515 wd->sc_badsect[i] = -1;
1516 }
1517 #endif
1518
1519 int
1520 wd_get_params(wd, flags, params)
1521 struct wd_softc *wd;
1522 u_int8_t flags;
1523 struct ataparams *params;
1524 {
1525 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1526 case CMD_AGAIN:
1527 return 1;
1528 case CMD_ERR:
1529 /*
1530 * We `know' there's a drive here; just assume it's old.
1531 * This geometry is only used to read the MBR and print a
1532 * (false) attach message.
1533 */
1534 strncpy(params->atap_model, "ST506",
1535 sizeof params->atap_model);
1536 params->atap_config = ATA_CFG_FIXED;
1537 params->atap_cylinders = 1024;
1538 params->atap_heads = 8;
1539 params->atap_sectors = 17;
1540 params->atap_multi = 1;
1541 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1542 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1543 return 0;
1544 case CMD_OK:
1545 return 0;
1546 default:
1547 panic("wd_get_params: bad return code from ata_get_params");
1548 /* NOTREACHED */
1549 }
1550 }
1551
1552 void
1553 wd_flushcache(wd, flags)
1554 struct wd_softc *wd;
1555 int flags;
1556 {
1557 struct wdc_command wdc_c;
1558
1559 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1560 return;
1561 memset(&wdc_c, 0, sizeof(struct wdc_command));
1562 wdc_c.r_command = WDCC_FLUSHCACHE;
1563 wdc_c.r_st_bmask = WDCS_DRDY;
1564 wdc_c.r_st_pmask = WDCS_DRDY;
1565 wdc_c.flags = flags;
1566 wdc_c.timeout = 30000; /* 30s timeout */
1567 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1568 printf("%s: flush cache command didn't complete\n",
1569 wd->sc_dev.dv_xname);
1570 }
1571 if (wdc_c.flags & AT_TIMEOU) {
1572 printf("%s: flush cache command timeout\n",
1573 wd->sc_dev.dv_xname);
1574 }
1575 if (wdc_c.flags & AT_DF) {
1576 printf("%s: flush cache command: drive fault\n",
1577 wd->sc_dev.dv_xname);
1578 }
1579 /*
1580 * Ignore error register, it shouldn't report anything else
1581 * than COMMAND ABORTED, which means the device doesn't support
1582 * flush cache
1583 */
1584 }
1585
1586 void
1587 wd_shutdown(arg)
1588 void *arg;
1589 {
1590 struct wd_softc *wd = arg;
1591 wd_flushcache(wd, AT_POLL);
1592 }
1593
1594 /*
1595 * Allocate space for a ioctl queue structure. Mostly taken from
1596 * scsipi_ioctl.c
1597 */
1598 struct wd_ioctl *
1599 wi_get()
1600 {
1601 struct wd_ioctl *wi;
1602 int s;
1603
1604 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1605 simple_lock_init(&wi->wi_bp.b_interlock);
1606 s = splbio();
1607 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1608 splx(s);
1609 return (wi);
1610 }
1611
1612 /*
1613 * Free an ioctl structure and remove it from our list
1614 */
1615
1616 void
1617 wi_free(wi)
1618 struct wd_ioctl *wi;
1619 {
1620 int s;
1621
1622 s = splbio();
1623 LIST_REMOVE(wi, wi_list);
1624 splx(s);
1625 free(wi, M_TEMP);
1626 }
1627
1628 /*
1629 * Find a wd_ioctl structure based on the struct buf.
1630 */
1631
1632 struct wd_ioctl *
1633 wi_find(bp)
1634 struct buf *bp;
1635 {
1636 struct wd_ioctl *wi;
1637 int s;
1638
1639 s = splbio();
1640 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1641 if (bp == &wi->wi_bp)
1642 break;
1643 splx(s);
1644 return (wi);
1645 }
1646
1647 /*
1648 * Ioctl pseudo strategy routine
1649 *
1650 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1651 * happens here is:
1652 *
1653 * - wdioctl() queues a wd_ioctl structure.
1654 *
1655 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1656 * user space I/O is required. If physio() is called, physio() eventually
1657 * calls wdioctlstrategy().
1658 *
1659 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1660 * to perform the actual command
1661 *
1662 * The reason for the use of the pseudo strategy routine is because
1663 * when doing I/O to/from user space, physio _really_ wants to be in
1664 * the loop. We could put the entire buffer into the ioctl request
1665 * structure, but that won't scale if we want to do things like download
1666 * microcode.
1667 */
1668
1669 void
1670 wdioctlstrategy(bp)
1671 struct buf *bp;
1672 {
1673 struct wd_ioctl *wi;
1674 struct wdc_command wdc_c;
1675 int error = 0;
1676
1677 wi = wi_find(bp);
1678 if (wi == NULL) {
1679 printf("user_strat: No ioctl\n");
1680 error = EINVAL;
1681 goto bad;
1682 }
1683
1684 memset(&wdc_c, 0, sizeof(wdc_c));
1685
1686 /*
1687 * Abort if physio broke up the transfer
1688 */
1689
1690 if (bp->b_bcount != wi->wi_atareq.datalen) {
1691 printf("physio split wd ioctl request... cannot proceed\n");
1692 error = EIO;
1693 goto bad;
1694 }
1695
1696 /*
1697 * Abort if we didn't get a buffer size that was a multiple of
1698 * our sector size (or was larger than NBBY)
1699 */
1700
1701 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1702 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1703 (1 << NBBY)) {
1704 error = EINVAL;
1705 goto bad;
1706 }
1707
1708 /*
1709 * Make sure a timeout was supplied in the ioctl request
1710 */
1711
1712 if (wi->wi_atareq.timeout == 0) {
1713 error = EINVAL;
1714 goto bad;
1715 }
1716
1717 if (wi->wi_atareq.flags & ATACMD_READ)
1718 wdc_c.flags |= AT_READ;
1719 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1720 wdc_c.flags |= AT_WRITE;
1721
1722 if (wi->wi_atareq.flags & ATACMD_READREG)
1723 wdc_c.flags |= AT_READREG;
1724
1725 wdc_c.flags |= AT_WAIT;
1726
1727 wdc_c.timeout = wi->wi_atareq.timeout;
1728 wdc_c.r_command = wi->wi_atareq.command;
1729 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1730 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1731 wdc_c.r_sector = wi->wi_atareq.sec_num;
1732 wdc_c.r_count = wi->wi_atareq.sec_count;
1733 wdc_c.r_precomp = wi->wi_atareq.features;
1734 wdc_c.r_st_bmask = WDCS_DRDY;
1735 wdc_c.r_st_pmask = WDCS_DRDY;
1736 wdc_c.data = wi->wi_bp.b_data;
1737 wdc_c.bcount = wi->wi_bp.b_bcount;
1738
1739 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
1740 != WDC_COMPLETE) {
1741 wi->wi_atareq.retsts = ATACMD_ERROR;
1742 goto bad;
1743 }
1744
1745 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1746 if (wdc_c.flags & AT_ERROR) {
1747 wi->wi_atareq.retsts = ATACMD_ERROR;
1748 wi->wi_atareq.error = wdc_c.r_error;
1749 } else if (wdc_c.flags & AT_DF)
1750 wi->wi_atareq.retsts = ATACMD_DF;
1751 else
1752 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1753 } else {
1754 wi->wi_atareq.retsts = ATACMD_OK;
1755 if (wi->wi_atareq.flags & ATACMD_READREG) {
1756 wi->wi_atareq.head = wdc_c.r_head ;
1757 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1758 wi->wi_atareq.sec_num = wdc_c.r_sector;
1759 wi->wi_atareq.sec_count = wdc_c.r_count;
1760 wi->wi_atareq.features = wdc_c.r_precomp;
1761 wi->wi_atareq.error = wdc_c.r_error;
1762 }
1763 }
1764
1765 bp->b_error = 0;
1766 biodone(bp);
1767 return;
1768 bad:
1769 bp->b_flags |= B_ERROR;
1770 bp->b_error = error;
1771 biodone(bp);
1772 }
1773