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