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