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