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