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