wd.c revision 1.232 1 /* $NetBSD: wd.c,v 1.232 2002/11/01 11:31:56 mrg 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.232 2002/11/01 11:31:56 mrg 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;
1009 #ifdef __HAVE_OLD_DISKLABEL
1010 struct disklabel newlabel;
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 = *(wd->sc_dk.dk_label);
1035 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1036 return ENOTTY;
1037 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1038 return 0;
1039 #endif
1040
1041 case DIOCGPART:
1042 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1043 ((struct partinfo *)addr)->part =
1044 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1045 return 0;
1046
1047 case DIOCWDINFO:
1048 case DIOCSDINFO:
1049 #ifdef __HAVE_OLD_DISKLABEL
1050 case ODIOCWDINFO:
1051 case ODIOCSDINFO:
1052 #endif
1053 {
1054 struct disklabel *lp;
1055
1056 #ifdef __HAVE_OLD_DISKLABEL
1057 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1058 memset(&newlabel, 0, sizeof newlabel);
1059 memcpy(&newlabel, addr, sizeof (struct olddisklabel));
1060 lp = &newlabel;
1061 } else
1062 #endif
1063 lp = (struct disklabel *)addr;
1064
1065 if ((flag & FWRITE) == 0)
1066 return EBADF;
1067
1068 if ((error = wdlock(wd)) != 0)
1069 return error;
1070 wd->sc_flags |= WDF_LABELLING;
1071
1072 error = setdisklabel(wd->sc_dk.dk_label,
1073 lp, /*wd->sc_dk.dk_openmask : */0,
1074 wd->sc_dk.dk_cpulabel);
1075 if (error == 0) {
1076 if (wd->drvp->state > RECAL)
1077 wd->drvp->drive_flags |= DRIVE_RESET;
1078 if (xfer == DIOCWDINFO
1079 #ifdef __HAVE_OLD_DISKLABEL
1080 || xfer == ODIOCWDINFO
1081 #endif
1082 )
1083 error = writedisklabel(WDLABELDEV(dev),
1084 wdstrategy, wd->sc_dk.dk_label,
1085 wd->sc_dk.dk_cpulabel);
1086 }
1087
1088 wd->sc_flags &= ~WDF_LABELLING;
1089 wdunlock(wd);
1090 return error;
1091 }
1092
1093 case DIOCKLABEL:
1094 if (*(int *)addr)
1095 wd->sc_flags |= WDF_KLABEL;
1096 else
1097 wd->sc_flags &= ~WDF_KLABEL;
1098 return 0;
1099
1100 case DIOCWLABEL:
1101 if ((flag & FWRITE) == 0)
1102 return EBADF;
1103 if (*(int *)addr)
1104 wd->sc_flags |= WDF_WLABEL;
1105 else
1106 wd->sc_flags &= ~WDF_WLABEL;
1107 return 0;
1108
1109 case DIOCGDEFLABEL:
1110 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1111 return 0;
1112 #ifdef __HAVE_OLD_DISKLABEL
1113 case ODIOCGDEFLABEL:
1114 wdgetdefaultlabel(wd, &newlabel);
1115 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
1116 return ENOTTY;
1117 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1118 return 0;
1119 #endif
1120
1121 #ifdef notyet
1122 case DIOCWFORMAT:
1123 if ((flag & FWRITE) == 0)
1124 return EBADF;
1125 {
1126 register struct format_op *fop;
1127 struct iovec aiov;
1128 struct uio auio;
1129
1130 fop = (struct format_op *)addr;
1131 aiov.iov_base = fop->df_buf;
1132 aiov.iov_len = fop->df_count;
1133 auio.uio_iov = &aiov;
1134 auio.uio_iovcnt = 1;
1135 auio.uio_resid = fop->df_count;
1136 auio.uio_segflg = 0;
1137 auio.uio_offset =
1138 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1139 auio.uio_procp = p;
1140 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1141 &auio);
1142 fop->df_count -= auio.uio_resid;
1143 fop->df_reg[0] = wdc->sc_status;
1144 fop->df_reg[1] = wdc->sc_error;
1145 return error;
1146 }
1147 #endif
1148
1149 case ATAIOCCOMMAND:
1150 /*
1151 * Make sure this command is (relatively) safe first
1152 */
1153 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1154 (flag & FWRITE) == 0)
1155 return (EBADF);
1156 {
1157 struct wd_ioctl *wi;
1158 atareq_t *atareq = (atareq_t *) addr;
1159 int error;
1160
1161 wi = wi_get();
1162 wi->wi_softc = wd;
1163 wi->wi_atareq = *atareq;
1164
1165 if (atareq->datalen && atareq->flags &
1166 (ATACMD_READ | ATACMD_WRITE)) {
1167 wi->wi_iov.iov_base = atareq->databuf;
1168 wi->wi_iov.iov_len = atareq->datalen;
1169 wi->wi_uio.uio_iov = &wi->wi_iov;
1170 wi->wi_uio.uio_iovcnt = 1;
1171 wi->wi_uio.uio_resid = atareq->datalen;
1172 wi->wi_uio.uio_offset = 0;
1173 wi->wi_uio.uio_segflg = UIO_USERSPACE;
1174 wi->wi_uio.uio_rw =
1175 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1176 wi->wi_uio.uio_procp = p;
1177 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
1178 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1179 minphys, &wi->wi_uio);
1180 } else {
1181 /* No need to call physio if we don't have any
1182 user data */
1183 wi->wi_bp.b_flags = 0;
1184 wi->wi_bp.b_data = 0;
1185 wi->wi_bp.b_bcount = 0;
1186 wi->wi_bp.b_dev = 0;
1187 wi->wi_bp.b_proc = p;
1188 wdioctlstrategy(&wi->wi_bp);
1189 error = wi->wi_bp.b_error;
1190 }
1191 *atareq = wi->wi_atareq;
1192 wi_free(wi);
1193 return(error);
1194 }
1195
1196 default:
1197 return ENOTTY;
1198 }
1199
1200 #ifdef DIAGNOSTIC
1201 panic("wdioctl: impossible");
1202 #endif
1203 }
1204
1205 #ifdef B_FORMAT
1206 int
1207 wdformat(struct buf *bp)
1208 {
1209
1210 bp->b_flags |= B_FORMAT;
1211 return wdstrategy(bp);
1212 }
1213 #endif
1214
1215 int
1216 wdsize(dev)
1217 dev_t dev;
1218 {
1219 struct wd_softc *wd;
1220 int part, omask;
1221 int size;
1222
1223 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1224
1225 wd = device_lookup(&wd_cd, WDUNIT(dev));
1226 if (wd == NULL)
1227 return (-1);
1228
1229 part = WDPART(dev);
1230 omask = wd->sc_dk.dk_openmask & (1 << part);
1231
1232 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1233 return (-1);
1234 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1235 size = -1;
1236 else
1237 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1238 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1239 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1240 return (-1);
1241 return (size);
1242 }
1243
1244 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1245 static int wddoingadump = 0;
1246 static int wddumprecalibrated = 0;
1247 static int wddumpmulti = 1;
1248
1249 /*
1250 * Dump core after a system crash.
1251 */
1252 int
1253 wddump(dev, blkno, va, size)
1254 dev_t dev;
1255 daddr_t blkno;
1256 caddr_t va;
1257 size_t size;
1258 {
1259 struct wd_softc *wd; /* disk unit to do the I/O */
1260 struct disklabel *lp; /* disk's disklabel */
1261 int part, err;
1262 int nblks; /* total number of sectors left to write */
1263
1264 /* Check if recursive dump; if so, punt. */
1265 if (wddoingadump)
1266 return EFAULT;
1267 wddoingadump = 1;
1268
1269 wd = device_lookup(&wd_cd, WDUNIT(dev));
1270 if (wd == NULL)
1271 return (ENXIO);
1272
1273 part = WDPART(dev);
1274
1275 /* Convert to disk sectors. Request must be a multiple of size. */
1276 lp = wd->sc_dk.dk_label;
1277 if ((size % lp->d_secsize) != 0)
1278 return EFAULT;
1279 nblks = size / lp->d_secsize;
1280 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1281
1282 /* Check transfer bounds against partition size. */
1283 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1284 return EINVAL;
1285
1286 /* Offset block number to start of partition. */
1287 blkno += lp->d_partitions[part].p_offset;
1288
1289 /* Recalibrate, if first dump transfer. */
1290 if (wddumprecalibrated == 0) {
1291 wddumpmulti = wd->sc_multi;
1292 wddumprecalibrated = 1;
1293 wd->drvp->state = RESET;
1294 }
1295
1296 while (nblks > 0) {
1297 again:
1298 wd->sc_bp = NULL;
1299 wd->sc_wdc_bio.blkno = blkno;
1300 wd->sc_wdc_bio.flags = ATA_POLL;
1301 if (wddumpmulti == 1)
1302 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1303 if (wd->sc_flags & WDF_LBA48)
1304 wd->sc_wdc_bio.flags |= ATA_LBA48;
1305 if (wd->sc_flags & WDF_LBA)
1306 wd->sc_wdc_bio.flags |= ATA_LBA;
1307 wd->sc_wdc_bio.bcount =
1308 min(nblks, wddumpmulti) * lp->d_secsize;
1309 wd->sc_wdc_bio.databuf = va;
1310 #ifndef WD_DUMP_NOT_TRUSTED
1311 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1312 case WDC_TRY_AGAIN:
1313 panic("wddump: try again");
1314 break;
1315 case WDC_QUEUED:
1316 panic("wddump: polled command has been queued");
1317 break;
1318 case WDC_COMPLETE:
1319 break;
1320 }
1321 switch(wd->sc_wdc_bio.error) {
1322 case TIMEOUT:
1323 printf("wddump: device timed out");
1324 err = EIO;
1325 break;
1326 case ERR_DF:
1327 printf("wddump: drive fault");
1328 err = EIO;
1329 break;
1330 case ERR_DMA:
1331 printf("wddump: DMA error");
1332 err = EIO;
1333 break;
1334 case ERROR:
1335 printf("wddump: ");
1336 wdperror(wd);
1337 err = EIO;
1338 break;
1339 case NOERROR:
1340 err = 0;
1341 break;
1342 default:
1343 panic("wddump: unknown error type");
1344 }
1345 if (err != 0) {
1346 if (wddumpmulti != 1) {
1347 wddumpmulti = 1; /* retry in single-sector */
1348 printf(", retrying\n");
1349 goto again;
1350 }
1351 printf("\n");
1352 return err;
1353 }
1354 #else /* WD_DUMP_NOT_TRUSTED */
1355 /* Let's just talk about this first... */
1356 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1357 unit, va, cylin, head, sector);
1358 delay(500 * 1000); /* half a second */
1359 #endif
1360
1361 /* update block count */
1362 nblks -= min(nblks, wddumpmulti);
1363 blkno += min(nblks, wddumpmulti);
1364 va += min(nblks, wddumpmulti) * lp->d_secsize;
1365 }
1366
1367 wddoingadump = 0;
1368 return 0;
1369 }
1370
1371 #ifdef HAS_BAD144_HANDLING
1372 /*
1373 * Internalize the bad sector table.
1374 */
1375 void
1376 bad144intern(wd)
1377 struct wd_softc *wd;
1378 {
1379 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1380 struct disklabel *lp = wd->sc_dk.dk_label;
1381 int i = 0;
1382
1383 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1384
1385 for (; i < NBT_BAD; i++) {
1386 if (bt->bt_bad[i].bt_cyl == 0xffff)
1387 break;
1388 wd->sc_badsect[i] =
1389 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1390 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1391 (bt->bt_bad[i].bt_trksec & 0xff);
1392 }
1393 for (; i < NBT_BAD+1; i++)
1394 wd->sc_badsect[i] = -1;
1395 }
1396 #endif
1397
1398 int
1399 wd_get_params(wd, flags, params)
1400 struct wd_softc *wd;
1401 u_int8_t flags;
1402 struct ataparams *params;
1403 {
1404 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1405 case CMD_AGAIN:
1406 return 1;
1407 case CMD_ERR:
1408 /*
1409 * We `know' there's a drive here; just assume it's old.
1410 * This geometry is only used to read the MBR and print a
1411 * (false) attach message.
1412 */
1413 strncpy(params->atap_model, "ST506",
1414 sizeof params->atap_model);
1415 params->atap_config = ATA_CFG_FIXED;
1416 params->atap_cylinders = 1024;
1417 params->atap_heads = 8;
1418 params->atap_sectors = 17;
1419 params->atap_multi = 1;
1420 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1421 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1422 return 0;
1423 case CMD_OK:
1424 return 0;
1425 default:
1426 panic("wd_get_params: bad return code from ata_get_params");
1427 /* NOTREACHED */
1428 }
1429 }
1430
1431 void
1432 wd_flushcache(wd, flags)
1433 struct wd_softc *wd;
1434 int flags;
1435 {
1436 struct wdc_command wdc_c;
1437
1438 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1439 return;
1440 memset(&wdc_c, 0, sizeof(struct wdc_command));
1441 wdc_c.r_command = WDCC_FLUSHCACHE;
1442 wdc_c.r_st_bmask = WDCS_DRDY;
1443 wdc_c.r_st_pmask = WDCS_DRDY;
1444 wdc_c.flags = flags;
1445 wdc_c.timeout = 30000; /* 30s timeout */
1446 if (wd->atabus->ata_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1447 printf("%s: flush cache command didn't complete\n",
1448 wd->sc_dev.dv_xname);
1449 }
1450 if (wdc_c.flags & AT_TIMEOU) {
1451 printf("%s: flush cache command timeout\n",
1452 wd->sc_dev.dv_xname);
1453 }
1454 if (wdc_c.flags & AT_DF) {
1455 printf("%s: flush cache command: drive fault\n",
1456 wd->sc_dev.dv_xname);
1457 }
1458 /*
1459 * Ignore error register, it shouldn't report anything else
1460 * than COMMAND ABORTED, which means the device doesn't support
1461 * flush cache
1462 */
1463 }
1464
1465 void
1466 wd_shutdown(arg)
1467 void *arg;
1468 {
1469 struct wd_softc *wd = arg;
1470 wd_flushcache(wd, AT_POLL);
1471 }
1472
1473 /*
1474 * Allocate space for a ioctl queue structure. Mostly taken from
1475 * scsipi_ioctl.c
1476 */
1477 struct wd_ioctl *
1478 wi_get()
1479 {
1480 struct wd_ioctl *wi;
1481 int s;
1482
1483 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1484 s = splbio();
1485 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1486 splx(s);
1487 return (wi);
1488 }
1489
1490 /*
1491 * Free an ioctl structure and remove it from our list
1492 */
1493
1494 void
1495 wi_free(wi)
1496 struct wd_ioctl *wi;
1497 {
1498 int s;
1499
1500 s = splbio();
1501 LIST_REMOVE(wi, wi_list);
1502 splx(s);
1503 free(wi, M_TEMP);
1504 }
1505
1506 /*
1507 * Find a wd_ioctl structure based on the struct buf.
1508 */
1509
1510 struct wd_ioctl *
1511 wi_find(bp)
1512 struct buf *bp;
1513 {
1514 struct wd_ioctl *wi;
1515 int s;
1516
1517 s = splbio();
1518 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1519 if (bp == &wi->wi_bp)
1520 break;
1521 splx(s);
1522 return (wi);
1523 }
1524
1525 /*
1526 * Ioctl pseudo strategy routine
1527 *
1528 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1529 * happens here is:
1530 *
1531 * - wdioctl() queues a wd_ioctl structure.
1532 *
1533 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1534 * user space I/O is required. If physio() is called, physio() eventually
1535 * calls wdioctlstrategy().
1536 *
1537 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1538 * to perform the actual command
1539 *
1540 * The reason for the use of the pseudo strategy routine is because
1541 * when doing I/O to/from user space, physio _really_ wants to be in
1542 * the loop. We could put the entire buffer into the ioctl request
1543 * structure, but that won't scale if we want to do things like download
1544 * microcode.
1545 */
1546
1547 void
1548 wdioctlstrategy(bp)
1549 struct buf *bp;
1550 {
1551 struct wd_ioctl *wi;
1552 struct wdc_command wdc_c;
1553 int error = 0;
1554
1555 wi = wi_find(bp);
1556 if (wi == NULL) {
1557 printf("user_strat: No ioctl\n");
1558 error = EINVAL;
1559 goto bad;
1560 }
1561
1562 memset(&wdc_c, 0, sizeof(wdc_c));
1563
1564 /*
1565 * Abort if physio broke up the transfer
1566 */
1567
1568 if (bp->b_bcount != wi->wi_atareq.datalen) {
1569 printf("physio split wd ioctl request... cannot proceed\n");
1570 error = EIO;
1571 goto bad;
1572 }
1573
1574 /*
1575 * Abort if we didn't get a buffer size that was a multiple of
1576 * our sector size (or was larger than NBBY)
1577 */
1578
1579 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1580 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1581 (1 << NBBY)) {
1582 error = EINVAL;
1583 goto bad;
1584 }
1585
1586 /*
1587 * Make sure a timeout was supplied in the ioctl request
1588 */
1589
1590 if (wi->wi_atareq.timeout == 0) {
1591 error = EINVAL;
1592 goto bad;
1593 }
1594
1595 if (wi->wi_atareq.flags & ATACMD_READ)
1596 wdc_c.flags |= AT_READ;
1597 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1598 wdc_c.flags |= AT_WRITE;
1599
1600 if (wi->wi_atareq.flags & ATACMD_READREG)
1601 wdc_c.flags |= AT_READREG;
1602
1603 wdc_c.flags |= AT_WAIT;
1604
1605 wdc_c.timeout = wi->wi_atareq.timeout;
1606 wdc_c.r_command = wi->wi_atareq.command;
1607 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1608 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1609 wdc_c.r_sector = wi->wi_atareq.sec_num;
1610 wdc_c.r_count = wi->wi_atareq.sec_count;
1611 wdc_c.r_precomp = wi->wi_atareq.features;
1612 wdc_c.r_st_bmask = WDCS_DRDY;
1613 wdc_c.r_st_pmask = WDCS_DRDY;
1614 wdc_c.data = wi->wi_bp.b_data;
1615 wdc_c.bcount = wi->wi_bp.b_bcount;
1616
1617 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &wdc_c)
1618 != WDC_COMPLETE) {
1619 wi->wi_atareq.retsts = ATACMD_ERROR;
1620 goto bad;
1621 }
1622
1623 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1624 if (wdc_c.flags & AT_ERROR) {
1625 wi->wi_atareq.retsts = ATACMD_ERROR;
1626 wi->wi_atareq.error = wdc_c.r_error;
1627 } else if (wdc_c.flags & AT_DF)
1628 wi->wi_atareq.retsts = ATACMD_DF;
1629 else
1630 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1631 } else {
1632 wi->wi_atareq.retsts = ATACMD_OK;
1633 if (wi->wi_atareq.flags & ATACMD_READREG) {
1634 wi->wi_atareq.head = wdc_c.r_head ;
1635 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1636 wi->wi_atareq.sec_num = wdc_c.r_sector;
1637 wi->wi_atareq.sec_count = wdc_c.r_count;
1638 wi->wi_atareq.features = wdc_c.r_precomp;
1639 wi->wi_atareq.error = wdc_c.r_error;
1640 }
1641 }
1642
1643 bp->b_error = 0;
1644 biodone(bp);
1645 return;
1646 bad:
1647 bp->b_flags |= B_ERROR;
1648 bp->b_error = error;
1649 biodone(bp);
1650 }
1651