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