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