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