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