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