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