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