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