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