wd.c revision 1.416 1 /* $NetBSD: wd.c,v 1.416 2014/12/31 17:06:48 christos 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.416 2014/12/31 17:06:48 christos 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 *, uint8_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 ((uint64_t) wd->sc_params.atap_max_lba[3] << 48) |
372 ((uint64_t) wd->sc_params.atap_max_lba[2] << 32) |
373 ((uint64_t) wd->sc_params.atap_max_lba[1] << 16) |
374 ((uint64_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
468 splx(s);
469
470 /* Detach disk. */
471 disk_detach(&sc->sc_dk);
472 disk_destroy(&sc->sc_dk);
473
474 #ifdef WD_SOFTBADSECT
475 /* Clean out the bad sector list */
476 while (!SLIST_EMPTY(&sc->sc_bslist)) {
477 void *head = SLIST_FIRST(&sc->sc_bslist);
478 SLIST_REMOVE_HEAD(&sc->sc_bslist, dbs_next);
479 free(head, M_TEMP);
480 }
481 sc->sc_bscount = 0;
482 #endif
483
484 pmf_device_deregister(self);
485
486 /* Unhook the entropy source. */
487 rnd_detach_source(&sc->rnd_source);
488
489 callout_destroy(&sc->sc_restart_ch);
490
491 sc->drvp->drive_type = ATA_DRIVET_NONE; /* no drive any more here */
492 sc->drvp->drive_flags = 0;
493
494 return (0);
495 }
496
497 /*
498 * Read/write routine for a buffer. Validates the arguments and schedules the
499 * transfer. Does not wait for the transfer to complete.
500 */
501 void
502 wdstrategy(struct buf *bp)
503 {
504 struct wd_softc *wd =
505 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
506 struct disklabel *lp = wd->sc_dk.dk_label;
507 daddr_t blkno;
508 int s;
509
510 ATADEBUG_PRINT(("wdstrategy (%s)\n", device_xname(wd->sc_dev)),
511 DEBUG_XFERS);
512
513 /* Valid request? */
514 if (bp->b_blkno < 0 ||
515 (bp->b_bcount % lp->d_secsize) != 0 ||
516 (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
517 bp->b_error = EINVAL;
518 goto done;
519 }
520
521 /* If device invalidated (e.g. media change, door open,
522 * device detachment), then error.
523 */
524 if ((wd->sc_flags & WDF_LOADED) == 0 ||
525 !device_is_enabled(wd->sc_dev)) {
526 bp->b_error = EIO;
527 goto done;
528 }
529
530 /* If it's a null transfer, return immediately. */
531 if (bp->b_bcount == 0)
532 goto done;
533
534 /*
535 * Do bounds checking, adjust transfer. if error, process.
536 * If end of partition, just return.
537 */
538 if (WDPART(bp->b_dev) == RAW_PART) {
539 if (bounds_check_with_mediasize(bp, DEV_BSIZE,
540 wd->sc_capacity) <= 0)
541 goto done;
542 } else {
543 if (bounds_check_with_label(&wd->sc_dk, bp,
544 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
545 goto done;
546 }
547
548 /*
549 * Now convert the block number to absolute and put it in
550 * terms of the device's logical block size.
551 */
552 if (lp->d_secsize >= DEV_BSIZE)
553 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
554 else
555 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
556
557 if (WDPART(bp->b_dev) != RAW_PART)
558 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
559
560 bp->b_rawblkno = blkno;
561
562 #ifdef WD_SOFTBADSECT
563 /*
564 * If the transfer about to be attempted contains only a block that
565 * is known to be bad then return an error for the transfer without
566 * even attempting to start a transfer up under the premis that we
567 * will just end up doing more retries for a transfer that will end
568 * up failing again.
569 * XXX:SMP - mutex required to protect with DIOCBSFLUSH
570 */
571 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
572 struct disk_badsectors *dbs;
573 daddr_t maxblk = blkno + (bp->b_bcount >> DEV_BSHIFT) - 1;
574
575 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
576 if ((dbs->dbs_min <= blkno && blkno <= dbs->dbs_max) ||
577 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
578 bp->b_error = EIO;
579 goto done;
580 }
581 }
582 #endif
583
584 /* Queue transfer on drive, activate drive and controller if idle. */
585 s = splbio();
586 bufq_put(wd->sc_q, bp);
587 wdstart(wd);
588 splx(s);
589 return;
590 done:
591 /* Toss transfer; we're done early. */
592 bp->b_resid = bp->b_bcount;
593 biodone(bp);
594 }
595
596 /*
597 * Queue a drive for I/O.
598 */
599 void
600 wdstart(void *arg)
601 {
602 struct wd_softc *wd = arg;
603 struct buf *bp = NULL;
604
605 ATADEBUG_PRINT(("wdstart %s\n", device_xname(wd->sc_dev)),
606 DEBUG_XFERS);
607
608 if (!device_is_active(wd->sc_dev))
609 return;
610
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 wdstart1(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 =
632 device_lookup_private(&wd_cd, DISKUNIT(obp->b_dev));
633 int s;
634
635 if (__predict_false(bp->b_error != 0)) {
636 /*
637 * Propagate the error. If this was the first half of
638 * the original transfer, make sure to account for that
639 * in the residual.
640 */
641 if (bp->b_data == obp->b_data)
642 bp->b_resid += bp->b_bcount;
643 goto done;
644 }
645
646 /*
647 * If this was the second half of the transfer, we're all done!
648 */
649 if (bp->b_data != obp->b_data)
650 goto done;
651
652 /*
653 * Advance the pointer to the second half and issue that command
654 * using the same opening.
655 */
656 bp->b_flags = obp->b_flags;
657 bp->b_oflags = obp->b_oflags;
658 bp->b_cflags = obp->b_cflags;
659 bp->b_data = (char *)bp->b_data + bp->b_bcount;
660 bp->b_blkno += (bp->b_bcount / 512);
661 bp->b_rawblkno += (bp->b_bcount / 512);
662 s = splbio();
663 wdstart1(sc, bp);
664 splx(s);
665 return;
666
667 done:
668 obp->b_error = bp->b_error;
669 obp->b_resid = bp->b_resid;
670 s = splbio();
671 putiobuf(bp);
672 biodone(obp);
673 sc->openings++;
674 splx(s);
675 /* wddone() will call wdstart() */
676 }
677
678 void
679 wdstart1(struct wd_softc *wd, struct buf *bp)
680 {
681
682 /*
683 * Deal with the "split mod15 write" quirk. We just divide the
684 * transfer in two, doing the first half and then then second half
685 * with the same command opening.
686 *
687 * Note we MUST do this here, because we can't let insertion
688 * into the bufq cause the transfers to be re-merged.
689 */
690 if (__predict_false((wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) != 0 &&
691 (bp->b_flags & B_READ) == 0 &&
692 bp->b_bcount > 512 &&
693 ((bp->b_bcount / 512) % 15) == 1)) {
694 struct buf *nbp;
695
696 /* already at splbio */
697 nbp = getiobuf(NULL, false);
698 if (__predict_false(nbp == NULL)) {
699 /* No memory -- fail the iop. */
700 bp->b_error = ENOMEM;
701 bp->b_resid = bp->b_bcount;
702 biodone(bp);
703 wd->openings++;
704 return;
705 }
706
707 nbp->b_error = 0;
708 nbp->b_proc = bp->b_proc;
709 nbp->b_dev = bp->b_dev;
710
711 nbp->b_bcount = bp->b_bcount / 2;
712 nbp->b_bufsize = bp->b_bcount / 2;
713 nbp->b_data = bp->b_data;
714
715 nbp->b_blkno = bp->b_blkno;
716 nbp->b_rawblkno = bp->b_rawblkno;
717
718 nbp->b_flags = bp->b_flags;
719 nbp->b_oflags = bp->b_oflags;
720 nbp->b_cflags = bp->b_cflags;
721 nbp->b_iodone = wd_split_mod15_write;
722
723 /* Put ptr to orig buf in b_private and use new buf */
724 nbp->b_private = bp;
725
726 BIO_COPYPRIO(nbp, bp);
727
728 bp = nbp;
729 }
730
731 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
732 wd->sc_wdc_bio.bcount = bp->b_bcount;
733 wd->sc_wdc_bio.databuf = bp->b_data;
734 wd->sc_wdc_bio.blkdone =0;
735 KASSERT(bp == wd->sc_bp || wd->sc_bp == NULL);
736 wd->sc_bp = bp;
737 /*
738 * If we're retrying, retry in single-sector mode. This will give us
739 * the sector number of the problem, and will eventually allow the
740 * transfer to succeed.
741 */
742 if (wd->retries >= WDIORETRIES_SINGLE)
743 wd->sc_wdc_bio.flags = ATA_SINGLE;
744 else
745 wd->sc_wdc_bio.flags = 0;
746 if (wd->sc_flags & WDF_LBA48 &&
747 (wd->sc_wdc_bio.blkno +
748 wd->sc_wdc_bio.bcount / wd->sc_dk.dk_label->d_secsize) >
749 wd->sc_capacity28)
750 wd->sc_wdc_bio.flags |= ATA_LBA48;
751 if (wd->sc_flags & WDF_LBA)
752 wd->sc_wdc_bio.flags |= ATA_LBA;
753 if (bp->b_flags & B_READ)
754 wd->sc_wdc_bio.flags |= ATA_READ;
755 /* Instrumentation. */
756 disk_busy(&wd->sc_dk);
757 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
758 case ATACMD_TRY_AGAIN:
759 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
760 break;
761 case ATACMD_QUEUED:
762 case ATACMD_COMPLETE:
763 break;
764 default:
765 panic("wdstart1: bad return code from ata_bio()");
766 }
767 }
768
769 void
770 wddone(void *v)
771 {
772 struct wd_softc *wd = device_private(v);
773 struct buf *bp = wd->sc_bp;
774 const char *errmsg;
775 int do_perror = 0;
776
777 ATADEBUG_PRINT(("wddone %s\n", device_xname(wd->sc_dev)),
778 DEBUG_XFERS);
779 if (bp == NULL)
780 return;
781 bp->b_resid = wd->sc_wdc_bio.bcount;
782 switch (wd->sc_wdc_bio.error) {
783 case ERR_DMA:
784 errmsg = "DMA error";
785 goto retry;
786 case ERR_DF:
787 errmsg = "device fault";
788 goto retry;
789 case TIMEOUT:
790 errmsg = "device timeout";
791 goto retry;
792 case ERR_RESET:
793 errmsg = "channel reset";
794 goto retry2;
795 case ERROR:
796 /* Don't care about media change bits */
797 if (wd->sc_wdc_bio.r_error != 0 &&
798 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
799 goto noerror;
800 errmsg = "error";
801 do_perror = 1;
802 retry: /* Just reset and retry. Can we do more ? */
803 (*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD, NULL);
804 retry2:
805 diskerr(bp, "wd", errmsg, LOG_PRINTF,
806 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
807 if (wd->retries < WDIORETRIES)
808 printf(", retrying");
809 printf("\n");
810 if (do_perror)
811 wdperror(wd);
812 if (wd->retries < WDIORETRIES) {
813 wd->retries++;
814 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
815 wdrestart, wd);
816 return;
817 }
818
819 #ifdef WD_SOFTBADSECT
820 /*
821 * Not all errors indicate a failed block but those that do,
822 * put the block on the bad-block list for the device. Only
823 * do this for reads because the drive should do it for writes,
824 * itself, according to Manuel.
825 */
826 if ((bp->b_flags & B_READ) &&
827 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
828 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
829 struct disk_badsectors *dbs;
830
831 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
832 dbs->dbs_min = bp->b_rawblkno;
833 dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
834 microtime(&dbs->dbs_failedat);
835 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
836 wd->sc_bscount++;
837 }
838 #endif
839 bp->b_error = EIO;
840 break;
841 case NOERROR:
842 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
843 aprint_error_dev(wd->sc_dev,
844 "soft error (corrected)\n");
845 break;
846 case ERR_NODEV:
847 bp->b_error = EIO;
848 break;
849 }
850 if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) {
851 /*
852 * the disk or controller sometimes report a complete
853 * xfer, when there has been an error. This is wrong,
854 * assume nothing got transfered in this case
855 */
856 bp->b_resid = bp->b_bcount;
857 }
858 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
859 (bp->b_flags & B_READ));
860 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
861 /* XXX Yuck, but we don't want to increment openings in this case */
862 if (__predict_false(bp->b_iodone == wd_split_mod15_write))
863 biodone(bp);
864 else {
865 biodone(bp);
866 wd->openings++;
867 }
868 KASSERT(wd->sc_bp != NULL);
869 wd->sc_bp = NULL;
870 wdstart(wd);
871 }
872
873 void
874 wdrestart(void *v)
875 {
876 struct wd_softc *wd = v;
877 struct buf *bp = wd->sc_bp;
878 int s;
879
880 ATADEBUG_PRINT(("wdrestart %s\n", device_xname(wd->sc_dev)),
881 DEBUG_XFERS);
882 s = splbio();
883 wdstart1(v, bp);
884 splx(s);
885 }
886
887 static void
888 wdminphys(struct buf *bp)
889 {
890
891 if (bp->b_bcount > (512 * 128)) {
892 bp->b_bcount = (512 * 128);
893 }
894 minphys(bp);
895 }
896
897 int
898 wdread(dev_t dev, struct uio *uio, int flags)
899 {
900
901 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
902 return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio));
903 }
904
905 int
906 wdwrite(dev_t dev, struct uio *uio, int flags)
907 {
908
909 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
910 return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio));
911 }
912
913 int
914 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
915 {
916 struct wd_softc *wd;
917 int part, error;
918
919 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
920 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
921 if (wd == NULL)
922 return (ENXIO);
923
924 if (! device_is_active(wd->sc_dev))
925 return (ENODEV);
926
927 if (wd->sc_capacity == 0)
928 return (ENODEV);
929
930 part = WDPART(dev);
931
932 mutex_enter(&wd->sc_dk.dk_openlock);
933
934 /*
935 * If there are wedges, and this is not RAW_PART, then we
936 * need to fail.
937 */
938 if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
939 error = EBUSY;
940 goto bad1;
941 }
942
943 /*
944 * If this is the first open of this device, add a reference
945 * to the adapter.
946 */
947 if (wd->sc_dk.dk_openmask == 0 &&
948 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
949 goto bad1;
950
951 if (wd->sc_dk.dk_openmask != 0) {
952 /*
953 * If any partition is open, but the disk has been invalidated,
954 * disallow further opens.
955 */
956 if ((wd->sc_flags & WDF_LOADED) == 0) {
957 error = EIO;
958 goto bad2;
959 }
960 } else {
961 if ((wd->sc_flags & WDF_LOADED) == 0) {
962
963 /* Load the physical device parameters. */
964 if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
965 aprint_error_dev(wd->sc_dev,
966 "IDENTIFY failed\n");
967 error = EIO;
968 goto bad2;
969 }
970 wd->sc_flags |= WDF_LOADED;
971 /* Load the partition info if not already loaded. */
972 wdgetdisklabel(wd);
973 }
974 }
975
976 /* Check that the partition exists. */
977 if (part != RAW_PART &&
978 (part >= wd->sc_dk.dk_label->d_npartitions ||
979 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
980 error = ENXIO;
981 goto bad2;
982 }
983
984 /* Insure only one open at a time. */
985 switch (fmt) {
986 case S_IFCHR:
987 wd->sc_dk.dk_copenmask |= (1 << part);
988 break;
989 case S_IFBLK:
990 wd->sc_dk.dk_bopenmask |= (1 << part);
991 break;
992 }
993 wd->sc_dk.dk_openmask =
994 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
995
996 mutex_exit(&wd->sc_dk.dk_openlock);
997 return 0;
998
999 bad2:
1000 if (wd->sc_dk.dk_openmask == 0)
1001 wd->atabus->ata_delref(wd->drvp);
1002 bad1:
1003 mutex_exit(&wd->sc_dk.dk_openlock);
1004 return error;
1005 }
1006
1007 /*
1008 * Caller must hold wd->sc_dk.dk_openlock.
1009 */
1010 static int
1011 wdlastclose(device_t self)
1012 {
1013 struct wd_softc *wd = device_private(self);
1014
1015 wd_flushcache(wd, AT_WAIT);
1016
1017 if (! (wd->sc_flags & WDF_KLABEL))
1018 wd->sc_flags &= ~WDF_LOADED;
1019
1020 wd->atabus->ata_delref(wd->drvp);
1021
1022 return 0;
1023 }
1024
1025 int
1026 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
1027 {
1028 struct wd_softc *wd =
1029 device_lookup_private(&wd_cd, WDUNIT(dev));
1030 int part = WDPART(dev);
1031
1032 ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
1033
1034 mutex_enter(&wd->sc_dk.dk_openlock);
1035
1036 switch (fmt) {
1037 case S_IFCHR:
1038 wd->sc_dk.dk_copenmask &= ~(1 << part);
1039 break;
1040 case S_IFBLK:
1041 wd->sc_dk.dk_bopenmask &= ~(1 << part);
1042 break;
1043 }
1044 wd->sc_dk.dk_openmask =
1045 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1046
1047 if (wd->sc_dk.dk_openmask == 0)
1048 wdlastclose(wd->sc_dev);
1049
1050 mutex_exit(&wd->sc_dk.dk_openlock);
1051 return 0;
1052 }
1053
1054 void
1055 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
1056 {
1057
1058 ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
1059 memset(lp, 0, sizeof(struct disklabel));
1060
1061 lp->d_secsize = DEV_BSIZE;
1062 lp->d_ntracks = wd->sc_params.atap_heads;
1063 lp->d_nsectors = wd->sc_params.atap_sectors;
1064 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
1065 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
1066 wd->sc_params.atap_cylinders;
1067 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1068
1069 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1070 lp->d_type = DTYPE_ST506;
1071 else
1072 lp->d_type = DTYPE_ESDI;
1073
1074 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1075 strncpy(lp->d_packname, "fictitious", 16);
1076 if (wd->sc_capacity > UINT32_MAX)
1077 lp->d_secperunit = UINT32_MAX;
1078 else
1079 lp->d_secperunit = wd->sc_capacity;
1080 lp->d_rpm = 3600;
1081 lp->d_interleave = 1;
1082 lp->d_flags = 0;
1083
1084 lp->d_partitions[RAW_PART].p_offset = 0;
1085 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1086 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1087 lp->d_npartitions = RAW_PART + 1;
1088
1089 lp->d_magic = DISKMAGIC;
1090 lp->d_magic2 = DISKMAGIC;
1091 lp->d_checksum = dkcksum(lp);
1092 }
1093
1094 /*
1095 * Fabricate a default disk label, and try to read the correct one.
1096 */
1097 void
1098 wdgetdisklabel(struct wd_softc *wd)
1099 {
1100 struct disklabel *lp = wd->sc_dk.dk_label;
1101 const char *errstring;
1102 int s;
1103
1104 ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1105
1106 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1107
1108 wdgetdefaultlabel(wd, lp);
1109
1110 wd->sc_badsect[0] = -1;
1111
1112 if (wd->drvp->state > RESET) {
1113 s = splbio();
1114 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1115 splx(s);
1116 }
1117 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1118 RAW_PART), wdstrategy, lp,
1119 wd->sc_dk.dk_cpulabel);
1120 if (errstring) {
1121 /*
1122 * This probably happened because the drive's default
1123 * geometry doesn't match the DOS geometry. We
1124 * assume the DOS geometry is now in the label and try
1125 * again. XXX This is a kluge.
1126 */
1127 if (wd->drvp->state > RESET) {
1128 s = splbio();
1129 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1130 splx(s);
1131 }
1132 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1133 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1134 }
1135 if (errstring) {
1136 aprint_error_dev(wd->sc_dev, "%s\n", errstring);
1137 return;
1138 }
1139
1140 if (wd->drvp->state > RESET) {
1141 s = splbio();
1142 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1143 splx(s);
1144 }
1145 #ifdef HAS_BAD144_HANDLING
1146 if ((lp->d_flags & D_BADSECT) != 0)
1147 bad144intern(wd);
1148 #endif
1149 }
1150
1151 void
1152 wdperror(const struct wd_softc *wd)
1153 {
1154 static const char *const errstr0_3[] = {"address mark not found",
1155 "track 0 not found", "aborted command", "media change requested",
1156 "id not found", "media changed", "uncorrectable data error",
1157 "bad block detected"};
1158 static const char *const errstr4_5[] = {
1159 "obsolete (address mark not found)",
1160 "no media/write protected", "aborted command",
1161 "media change requested", "id not found", "media changed",
1162 "uncorrectable data error", "interface CRC error"};
1163 const char *const *errstr;
1164 int i;
1165 const char *sep = "";
1166
1167 const char *devname = device_xname(wd->sc_dev);
1168 struct ata_drive_datas *drvp = wd->drvp;
1169 int errno = wd->sc_wdc_bio.r_error;
1170
1171 if (drvp->ata_vers >= 4)
1172 errstr = errstr4_5;
1173 else
1174 errstr = errstr0_3;
1175
1176 printf("%s: (", devname);
1177
1178 if (errno == 0)
1179 printf("error not notified");
1180
1181 for (i = 0; i < 8; i++) {
1182 if (errno & (1 << i)) {
1183 printf("%s%s", sep, errstr[i]);
1184 sep = ", ";
1185 }
1186 }
1187 printf(")\n");
1188 }
1189
1190 int
1191 wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
1192 {
1193 struct wd_softc *wd =
1194 device_lookup_private(&wd_cd, WDUNIT(dev));
1195 int error, s;
1196 #ifdef __HAVE_OLD_DISKLABEL
1197 struct disklabel *newlabel = NULL;
1198 #endif
1199
1200 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1201
1202 if ((wd->sc_flags & WDF_LOADED) == 0)
1203 return EIO;
1204
1205 error = disk_ioctl(&wd->sc_dk, xfer, addr, flag, l);
1206 if (error != EPASSTHROUGH)
1207 return (error);
1208
1209 error = 0;
1210 switch (xfer) {
1211 #ifdef HAS_BAD144_HANDLING
1212 case DIOCSBAD:
1213 if ((flag & FWRITE) == 0)
1214 return EBADF;
1215 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1216 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1217 bad144intern(wd);
1218 return 0;
1219 #endif
1220 #ifdef WD_SOFTBADSECT
1221 case DIOCBSLIST :
1222 {
1223 uint32_t count, missing, skip;
1224 struct disk_badsecinfo dbsi;
1225 struct disk_badsectors *dbs;
1226 size_t available;
1227 uint8_t *laddr;
1228
1229 dbsi = *(struct disk_badsecinfo *)addr;
1230 missing = wd->sc_bscount;
1231 count = 0;
1232 available = dbsi.dbsi_bufsize;
1233 skip = dbsi.dbsi_skip;
1234 laddr = (uint8_t *)dbsi.dbsi_buffer;
1235
1236 /*
1237 * We start this loop with the expectation that all of the
1238 * entries will be missed and decrement this counter each
1239 * time we either skip over one (already copied out) or
1240 * we actually copy it back to user space. The structs
1241 * holding the bad sector information are copied directly
1242 * back to user space whilst the summary is returned via
1243 * the struct passed in via the ioctl.
1244 */
1245 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1246 if (skip > 0) {
1247 missing--;
1248 skip--;
1249 continue;
1250 }
1251 if (available < sizeof(*dbs))
1252 break;
1253 available -= sizeof(*dbs);
1254 copyout(dbs, laddr, sizeof(*dbs));
1255 laddr += sizeof(*dbs);
1256 missing--;
1257 count++;
1258 }
1259 dbsi.dbsi_left = missing;
1260 dbsi.dbsi_copied = count;
1261 *(struct disk_badsecinfo *)addr = dbsi;
1262 return 0;
1263 }
1264
1265 case DIOCBSFLUSH :
1266 /* Clean out the bad sector list */
1267 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1268 void *head = SLIST_FIRST(&wd->sc_bslist);
1269 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1270 free(head, M_TEMP);
1271 }
1272 wd->sc_bscount = 0;
1273 return 0;
1274 #endif
1275 case DIOCGDINFO:
1276 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1277 return 0;
1278 #ifdef __HAVE_OLD_DISKLABEL
1279 case ODIOCGDINFO:
1280 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1281 if (newlabel == NULL)
1282 return EIO;
1283 *newlabel = *(wd->sc_dk.dk_label);
1284 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1285 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1286 else
1287 error = ENOTTY;
1288 free(newlabel, M_TEMP);
1289 return error;
1290 #endif
1291
1292 case DIOCGPART:
1293 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1294 ((struct partinfo *)addr)->part =
1295 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1296 return 0;
1297
1298 case DIOCWDINFO:
1299 case DIOCSDINFO:
1300 #ifdef __HAVE_OLD_DISKLABEL
1301 case ODIOCWDINFO:
1302 case ODIOCSDINFO:
1303 #endif
1304 {
1305 struct disklabel *lp;
1306
1307 if ((flag & FWRITE) == 0)
1308 return EBADF;
1309
1310 #ifdef __HAVE_OLD_DISKLABEL
1311 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1312 newlabel = malloc(sizeof *newlabel, M_TEMP,
1313 M_WAITOK | M_ZERO);
1314 if (newlabel == NULL)
1315 return EIO;
1316 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1317 lp = newlabel;
1318 } else
1319 #endif
1320 lp = (struct disklabel *)addr;
1321
1322 mutex_enter(&wd->sc_dk.dk_openlock);
1323 wd->sc_flags |= WDF_LABELLING;
1324
1325 error = setdisklabel(wd->sc_dk.dk_label,
1326 lp, /*wd->sc_dk.dk_openmask : */0,
1327 wd->sc_dk.dk_cpulabel);
1328 if (error == 0) {
1329 if (wd->drvp->state > RESET) {
1330 s = splbio();
1331 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1332 splx(s);
1333 }
1334 if (xfer == DIOCWDINFO
1335 #ifdef __HAVE_OLD_DISKLABEL
1336 || xfer == ODIOCWDINFO
1337 #endif
1338 )
1339 error = writedisklabel(WDLABELDEV(dev),
1340 wdstrategy, wd->sc_dk.dk_label,
1341 wd->sc_dk.dk_cpulabel);
1342 }
1343
1344 wd->sc_flags &= ~WDF_LABELLING;
1345 mutex_exit(&wd->sc_dk.dk_openlock);
1346 #ifdef __HAVE_OLD_DISKLABEL
1347 if (newlabel != NULL)
1348 free(newlabel, M_TEMP);
1349 #endif
1350 return error;
1351 }
1352
1353 case DIOCKLABEL:
1354 if (*(int *)addr)
1355 wd->sc_flags |= WDF_KLABEL;
1356 else
1357 wd->sc_flags &= ~WDF_KLABEL;
1358 return 0;
1359
1360 case DIOCWLABEL:
1361 if ((flag & FWRITE) == 0)
1362 return EBADF;
1363 if (*(int *)addr)
1364 wd->sc_flags |= WDF_WLABEL;
1365 else
1366 wd->sc_flags &= ~WDF_WLABEL;
1367 return 0;
1368
1369 case DIOCGDEFLABEL:
1370 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1371 return 0;
1372 #ifdef __HAVE_OLD_DISKLABEL
1373 case ODIOCGDEFLABEL:
1374 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1375 if (newlabel == NULL)
1376 return EIO;
1377 wdgetdefaultlabel(wd, newlabel);
1378 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1379 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1380 else
1381 error = ENOTTY;
1382 free(newlabel, M_TEMP);
1383 return error;
1384 #endif
1385
1386 #ifdef notyet
1387 case DIOCWFORMAT:
1388 if ((flag & FWRITE) == 0)
1389 return EBADF;
1390 {
1391 register struct format_op *fop;
1392 struct iovec aiov;
1393 struct uio auio;
1394
1395 fop = (struct format_op *)addr;
1396 aiov.iov_base = fop->df_buf;
1397 aiov.iov_len = fop->df_count;
1398 auio.uio_iov = &aiov;
1399 auio.uio_iovcnt = 1;
1400 auio.uio_resid = fop->df_count;
1401 auio.uio_offset =
1402 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1403 auio.uio_vmspace = l->l_proc->p_vmspace;
1404 error = physio(wdformat, NULL, dev, B_WRITE, wdminphys,
1405 &auio);
1406 fop->df_count -= auio.uio_resid;
1407 fop->df_reg[0] = wdc->sc_status;
1408 fop->df_reg[1] = wdc->sc_error;
1409 return error;
1410 }
1411 #endif
1412 case DIOCGCACHE:
1413 return wd_getcache(wd, (int *)addr);
1414
1415 case DIOCSCACHE:
1416 return wd_setcache(wd, *(int *)addr);
1417
1418 case DIOCCACHESYNC:
1419 return wd_flushcache(wd, AT_WAIT);
1420
1421 case ATAIOCCOMMAND:
1422 /*
1423 * Make sure this command is (relatively) safe first
1424 */
1425 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1426 (flag & FWRITE) == 0)
1427 return (EBADF);
1428 {
1429 struct wd_ioctl *wi;
1430 atareq_t *atareq = (atareq_t *) addr;
1431 int error1;
1432
1433 wi = wi_get();
1434 wi->wi_softc = wd;
1435 wi->wi_atareq = *atareq;
1436
1437 if (atareq->datalen && atareq->flags &
1438 (ATACMD_READ | ATACMD_WRITE)) {
1439 void *tbuf;
1440 if (atareq->datalen < DEV_BSIZE
1441 && atareq->command == WDCC_IDENTIFY) {
1442 tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1443 wi->wi_iov.iov_base = tbuf;
1444 wi->wi_iov.iov_len = DEV_BSIZE;
1445 UIO_SETUP_SYSSPACE(&wi->wi_uio);
1446 } else {
1447 tbuf = NULL;
1448 wi->wi_iov.iov_base = atareq->databuf;
1449 wi->wi_iov.iov_len = atareq->datalen;
1450 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1451 }
1452 wi->wi_uio.uio_iov = &wi->wi_iov;
1453 wi->wi_uio.uio_iovcnt = 1;
1454 wi->wi_uio.uio_resid = atareq->datalen;
1455 wi->wi_uio.uio_offset = 0;
1456 wi->wi_uio.uio_rw =
1457 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1458 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1459 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1460 wdminphys, &wi->wi_uio);
1461 if (tbuf != NULL && error1 == 0) {
1462 error1 = copyout(tbuf, atareq->databuf,
1463 atareq->datalen);
1464 free(tbuf, M_TEMP);
1465 }
1466 } else {
1467 /* No need to call physio if we don't have any
1468 user data */
1469 wi->wi_bp.b_flags = 0;
1470 wi->wi_bp.b_data = 0;
1471 wi->wi_bp.b_bcount = 0;
1472 wi->wi_bp.b_dev = 0;
1473 wi->wi_bp.b_proc = l->l_proc;
1474 wdioctlstrategy(&wi->wi_bp);
1475 error1 = wi->wi_bp.b_error;
1476 }
1477 *atareq = wi->wi_atareq;
1478 wi_free(wi);
1479 return(error1);
1480 }
1481
1482 case DIOCGSTRATEGY:
1483 {
1484 struct disk_strategy *dks = (void *)addr;
1485
1486 s = splbio();
1487 strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q),
1488 sizeof(dks->dks_name));
1489 splx(s);
1490 dks->dks_paramlen = 0;
1491
1492 return 0;
1493 }
1494
1495 case DIOCSSTRATEGY:
1496 {
1497 struct disk_strategy *dks = (void *)addr;
1498 struct bufq_state *new;
1499 struct bufq_state *old;
1500
1501 if ((flag & FWRITE) == 0) {
1502 return EBADF;
1503 }
1504 if (dks->dks_param != NULL) {
1505 return EINVAL;
1506 }
1507 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1508 error = bufq_alloc(&new, dks->dks_name,
1509 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1510 if (error) {
1511 return error;
1512 }
1513 s = splbio();
1514 old = wd->sc_q;
1515 bufq_move(new, old);
1516 wd->sc_q = new;
1517 splx(s);
1518 bufq_free(old);
1519
1520 return 0;
1521 }
1522
1523 default:
1524 return ENOTTY;
1525 }
1526
1527 #ifdef DIAGNOSTIC
1528 panic("wdioctl: impossible");
1529 #endif
1530 }
1531
1532 static int
1533 wddiscard(dev_t dev, off_t pos, off_t len)
1534 {
1535 struct wd_softc *wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1536 daddr_t bno;
1537 long size, done;
1538 long maxatonce, amount;
1539 int result;
1540
1541 if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7)
1542 || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) {
1543 /* not supported; ignore request */
1544 ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS);
1545 return 0;
1546 }
1547 maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/
1548
1549 ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS);
1550
1551 if ((wd->sc_flags & WDF_LOADED) == 0)
1552 return EIO;
1553
1554 /* round the start up and the end down */
1555 bno = (pos + DEV_BSIZE - 1) >> DEV_BSHIFT;
1556 size = ((pos + len) >> DEV_BSHIFT) - bno;
1557
1558 done = 0;
1559 while (done < size) {
1560 amount = size - done;
1561 if (amount > maxatonce) {
1562 amount = maxatonce;
1563 }
1564 result = wd_trim(wd, WDPART(dev), bno + done, amount);
1565 if (result) {
1566 return result;
1567 }
1568 done += amount;
1569 }
1570 return 0;
1571 }
1572
1573 #ifdef B_FORMAT
1574 int
1575 wdformat(struct buf *bp)
1576 {
1577
1578 bp->b_flags |= B_FORMAT;
1579 return wdstrategy(bp);
1580 }
1581 #endif
1582
1583 int
1584 wdsize(dev_t dev)
1585 {
1586 struct wd_softc *wd;
1587 int part, omask;
1588 int size;
1589
1590 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1591
1592 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1593 if (wd == NULL)
1594 return (-1);
1595
1596 part = WDPART(dev);
1597 omask = wd->sc_dk.dk_openmask & (1 << part);
1598
1599 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1600 return (-1);
1601 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1602 size = -1;
1603 else
1604 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1605 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1606 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1607 return (-1);
1608 return (size);
1609 }
1610
1611 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1612 static int wddoingadump = 0;
1613 static int wddumprecalibrated = 0;
1614
1615 /*
1616 * Dump core after a system crash.
1617 */
1618 int
1619 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1620 {
1621 struct wd_softc *wd; /* disk unit to do the I/O */
1622 struct disklabel *lp; /* disk's disklabel */
1623 int part, err;
1624 int nblks; /* total number of sectors left to write */
1625
1626 /* Check if recursive dump; if so, punt. */
1627 if (wddoingadump)
1628 return EFAULT;
1629 wddoingadump = 1;
1630
1631 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1632 if (wd == NULL)
1633 return (ENXIO);
1634
1635 part = WDPART(dev);
1636
1637 /* Convert to disk sectors. Request must be a multiple of size. */
1638 lp = wd->sc_dk.dk_label;
1639 if ((size % lp->d_secsize) != 0)
1640 return EFAULT;
1641 nblks = size / lp->d_secsize;
1642 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1643
1644 /* Check transfer bounds against partition size. */
1645 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1646 return EINVAL;
1647
1648 /* Offset block number to start of partition. */
1649 blkno += lp->d_partitions[part].p_offset;
1650
1651 /* Recalibrate, if first dump transfer. */
1652 if (wddumprecalibrated == 0) {
1653 wddumprecalibrated = 1;
1654 (*wd->atabus->ata_reset_drive)(wd->drvp,
1655 AT_POLL | AT_RST_EMERG, NULL);
1656 wd->drvp->state = RESET;
1657 }
1658
1659 wd->sc_bp = NULL;
1660 wd->sc_wdc_bio.blkno = blkno;
1661 wd->sc_wdc_bio.flags = ATA_POLL;
1662 if (wd->sc_flags & WDF_LBA48 &&
1663 (wd->sc_wdc_bio.blkno + nblks) > wd->sc_capacity28)
1664 wd->sc_wdc_bio.flags |= ATA_LBA48;
1665 if (wd->sc_flags & WDF_LBA)
1666 wd->sc_wdc_bio.flags |= ATA_LBA;
1667 wd->sc_wdc_bio.bcount = nblks * lp->d_secsize;
1668 wd->sc_wdc_bio.databuf = va;
1669 #ifndef WD_DUMP_NOT_TRUSTED
1670 switch (err = wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1671 case ATACMD_TRY_AGAIN:
1672 panic("wddump: try again");
1673 break;
1674 case ATACMD_QUEUED:
1675 panic("wddump: polled command has been queued");
1676 break;
1677 case ATACMD_COMPLETE:
1678 break;
1679 default:
1680 panic("wddump: unknown atacmd code %d", err);
1681 }
1682 switch(err = wd->sc_wdc_bio.error) {
1683 case TIMEOUT:
1684 printf("wddump: device timed out");
1685 err = EIO;
1686 break;
1687 case ERR_DF:
1688 printf("wddump: drive fault");
1689 err = EIO;
1690 break;
1691 case ERR_DMA:
1692 printf("wddump: DMA error");
1693 err = EIO;
1694 break;
1695 case ERROR:
1696 printf("wddump: ");
1697 wdperror(wd);
1698 err = EIO;
1699 break;
1700 case NOERROR:
1701 err = 0;
1702 break;
1703 default:
1704 panic("wddump: unknown error type %d", err);
1705 }
1706 if (err != 0) {
1707 printf("\n");
1708 return err;
1709 }
1710 #else /* WD_DUMP_NOT_TRUSTED */
1711 /* Let's just talk about this first... */
1712 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1713 unit, va, cylin, head, sector);
1714 delay(500 * 1000); /* half a second */
1715 #endif
1716
1717 wddoingadump = 0;
1718 return 0;
1719 }
1720
1721 #ifdef HAS_BAD144_HANDLING
1722 /*
1723 * Internalize the bad sector table.
1724 */
1725 void
1726 bad144intern(struct wd_softc *wd)
1727 {
1728 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1729 struct disklabel *lp = wd->sc_dk.dk_label;
1730 int i = 0;
1731
1732 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1733
1734 for (; i < NBT_BAD; i++) {
1735 if (bt->bt_bad[i].bt_cyl == 0xffff)
1736 break;
1737 wd->sc_badsect[i] =
1738 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1739 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1740 (bt->bt_bad[i].bt_trksec & 0xff);
1741 }
1742 for (; i < NBT_BAD+1; i++)
1743 wd->sc_badsect[i] = -1;
1744 }
1745 #endif
1746
1747 static void
1748 wd_params_to_properties(struct wd_softc *wd, struct ataparams *params)
1749 {
1750 struct disk_geom *dg = &wd->sc_dk.dk_geom;
1751
1752 memset(dg, 0, sizeof(*dg));
1753
1754 dg->dg_secperunit = wd->sc_capacity;
1755 dg->dg_secsize = DEV_BSIZE /* XXX 512? */;
1756 dg->dg_nsectors = wd->sc_params.atap_sectors;
1757 dg->dg_ntracks = wd->sc_params.atap_heads;
1758 if ((wd->sc_flags & WDF_LBA) == 0)
1759 dg->dg_ncylinders = wd->sc_params.atap_cylinders;
1760
1761 /* XXX Should have a case for ATA here, too. */
1762 const char *cp = strcmp(wd->sc_params.atap_model, "ST506") ?
1763 "ST506" : "ESDI";
1764
1765 disk_set_info(wd->sc_dev, &wd->sc_dk, cp);
1766 }
1767
1768 int
1769 wd_get_params(struct wd_softc *wd, uint8_t flags, struct ataparams *params)
1770 {
1771
1772 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1773 case CMD_AGAIN:
1774 return 1;
1775 case CMD_ERR:
1776 if (wd->drvp->drive_type != ATA_DRIVET_OLD)
1777 return 1;
1778 /*
1779 * We `know' there's a drive here; just assume it's old.
1780 * This geometry is only used to read the MBR and print a
1781 * (false) attach message.
1782 */
1783 strncpy(params->atap_model, "ST506",
1784 sizeof params->atap_model);
1785 params->atap_config = ATA_CFG_FIXED;
1786 params->atap_cylinders = 1024;
1787 params->atap_heads = 8;
1788 params->atap_sectors = 17;
1789 params->atap_multi = 1;
1790 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1791 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1792 /* FALLTHROUGH */
1793 case CMD_OK:
1794 wd_params_to_properties(wd, params);
1795 return 0;
1796 default:
1797 panic("wd_get_params: bad return code from ata_get_params");
1798 /* NOTREACHED */
1799 }
1800 }
1801
1802 int
1803 wd_getcache(struct wd_softc *wd, int *bitsp)
1804 {
1805 struct ataparams params;
1806
1807 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1808 return EIO;
1809 if (params.atap_cmd_set1 == 0x0000 ||
1810 params.atap_cmd_set1 == 0xffff ||
1811 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1812 *bitsp = 0;
1813 return 0;
1814 }
1815 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1816 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1817 *bitsp |= DKCACHE_WRITE;
1818
1819 return 0;
1820 }
1821
1822 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1823
1824 int
1825 wd_setcache(struct wd_softc *wd, int bits)
1826 {
1827 struct ataparams params;
1828 struct ata_command ata_c;
1829
1830 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1831 return EIO;
1832
1833 if (params.atap_cmd_set1 == 0x0000 ||
1834 params.atap_cmd_set1 == 0xffff ||
1835 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1836 return EOPNOTSUPP;
1837
1838 if ((bits & DKCACHE_READ) == 0 ||
1839 (bits & DKCACHE_SAVE) != 0)
1840 return EOPNOTSUPP;
1841
1842 memset(&ata_c, 0, sizeof(struct ata_command));
1843 ata_c.r_command = SET_FEATURES;
1844 ata_c.r_st_bmask = 0;
1845 ata_c.r_st_pmask = 0;
1846 ata_c.timeout = 30000; /* 30s timeout */
1847 ata_c.flags = AT_WAIT;
1848 if (bits & DKCACHE_WRITE)
1849 ata_c.r_features = WDSF_WRITE_CACHE_EN;
1850 else
1851 ata_c.r_features = WDSF_WRITE_CACHE_DS;
1852 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1853 aprint_error_dev(wd->sc_dev,
1854 "wd_setcache command not complete\n");
1855 return EIO;
1856 }
1857 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1858 char sbuf[sizeof(at_errbits) + 64];
1859 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1860 aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n", sbuf);
1861 return EIO;
1862 }
1863 return 0;
1864 }
1865
1866 static int
1867 wd_standby(struct wd_softc *wd, int flags)
1868 {
1869 struct ata_command ata_c;
1870
1871 memset(&ata_c, 0, sizeof(struct ata_command));
1872 ata_c.r_command = WDCC_STANDBY_IMMED;
1873 ata_c.r_st_bmask = WDCS_DRDY;
1874 ata_c.r_st_pmask = WDCS_DRDY;
1875 ata_c.flags = flags;
1876 ata_c.timeout = 30000; /* 30s timeout */
1877 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1878 aprint_error_dev(wd->sc_dev,
1879 "standby immediate command didn't complete\n");
1880 return EIO;
1881 }
1882 if (ata_c.flags & AT_ERROR) {
1883 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1884 return ENODEV;
1885 }
1886 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1887 char sbuf[sizeof(at_errbits) + 64];
1888 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1889 aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n", sbuf);
1890 return EIO;
1891 }
1892 return 0;
1893 }
1894
1895 int
1896 wd_flushcache(struct wd_softc *wd, int flags)
1897 {
1898 struct ata_command ata_c;
1899
1900 /*
1901 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1902 * only ATA-2 and still support it.
1903 */
1904 if (wd->drvp->ata_vers < 4 &&
1905 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1906 wd->sc_params.atap_cmd_set2 == 0xffff))
1907 return ENODEV;
1908 memset(&ata_c, 0, sizeof(struct ata_command));
1909 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1910 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) {
1911 ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1912 flags |= AT_LBA48;
1913 } else
1914 ata_c.r_command = WDCC_FLUSHCACHE;
1915 ata_c.r_st_bmask = WDCS_DRDY;
1916 ata_c.r_st_pmask = WDCS_DRDY;
1917 ata_c.flags = flags | AT_READREG;
1918 ata_c.timeout = 300000; /* 5m timeout */
1919 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1920 aprint_error_dev(wd->sc_dev,
1921 "flush cache command didn't complete\n");
1922 return EIO;
1923 }
1924 if (ata_c.flags & AT_ERROR) {
1925 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1926 return ENODEV;
1927 }
1928 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1929 char sbuf[sizeof(at_errbits) + 64];
1930 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1931 aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n",
1932 sbuf);
1933 return EIO;
1934 }
1935 return 0;
1936 }
1937
1938 int
1939 wd_trim(struct wd_softc *wd, int part, daddr_t bno, long size)
1940 {
1941 struct ata_command ata_c;
1942 unsigned char *req;
1943
1944 if (part != RAW_PART)
1945 bno += wd->sc_dk.dk_label->d_partitions[part].p_offset;;
1946
1947 req = kmem_zalloc(512, KM_SLEEP);
1948 req[0] = bno & 0xff;
1949 req[1] = (bno >> 8) & 0xff;
1950 req[2] = (bno >> 16) & 0xff;
1951 req[3] = (bno >> 24) & 0xff;
1952 req[4] = (bno >> 32) & 0xff;
1953 req[5] = (bno >> 40) & 0xff;
1954 req[6] = size & 0xff;
1955 req[7] = (size >> 8) & 0xff;
1956
1957 memset(&ata_c, 0, sizeof(struct ata_command));
1958 ata_c.r_command = ATA_DATA_SET_MANAGEMENT;
1959 ata_c.r_count = 1;
1960 ata_c.r_features = ATA_SUPPORT_DSM_TRIM;
1961 ata_c.r_st_bmask = WDCS_DRDY;
1962 ata_c.r_st_pmask = WDCS_DRDY;
1963 ata_c.timeout = 30000; /* 30s timeout */
1964 ata_c.data = req;
1965 ata_c.bcount = 512;
1966 ata_c.flags |= AT_WRITE | AT_WAIT;
1967 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1968 aprint_error_dev(wd->sc_dev,
1969 "trim command didn't complete\n");
1970 kmem_free(req, 512);
1971 return EIO;
1972 }
1973 kmem_free(req, 512);
1974 if (ata_c.flags & AT_ERROR) {
1975 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1976 return ENODEV;
1977 }
1978 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1979 char sbuf[sizeof(at_errbits) + 64];
1980 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1981 aprint_error_dev(wd->sc_dev, "wd_trim: status=%s\n",
1982 sbuf);
1983 return EIO;
1984 }
1985 return 0;
1986 }
1987
1988 bool
1989 wd_shutdown(device_t dev, int how)
1990 {
1991 struct wd_softc *wd = device_private(dev);
1992
1993 /* the adapter needs to be enabled */
1994 if (wd->atabus->ata_addref(wd->drvp))
1995 return true; /* no need to complain */
1996
1997 wd_flushcache(wd, AT_POLL);
1998 if ((how & RB_POWERDOWN) == RB_POWERDOWN)
1999 wd_standby(wd, AT_POLL);
2000 return true;
2001 }
2002
2003 /*
2004 * Allocate space for a ioctl queue structure. Mostly taken from
2005 * scsipi_ioctl.c
2006 */
2007 struct wd_ioctl *
2008 wi_get(void)
2009 {
2010 struct wd_ioctl *wi;
2011 int s;
2012
2013 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
2014 buf_init(&wi->wi_bp);
2015 s = splbio();
2016 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
2017 splx(s);
2018 return (wi);
2019 }
2020
2021 /*
2022 * Free an ioctl structure and remove it from our list
2023 */
2024
2025 void
2026 wi_free(struct wd_ioctl *wi)
2027 {
2028 int s;
2029
2030 s = splbio();
2031 LIST_REMOVE(wi, wi_list);
2032 splx(s);
2033 buf_destroy(&wi->wi_bp);
2034 free(wi, M_TEMP);
2035 }
2036
2037 /*
2038 * Find a wd_ioctl structure based on the struct buf.
2039 */
2040
2041 struct wd_ioctl *
2042 wi_find(struct buf *bp)
2043 {
2044 struct wd_ioctl *wi;
2045 int s;
2046
2047 s = splbio();
2048 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
2049 if (bp == &wi->wi_bp)
2050 break;
2051 splx(s);
2052 return (wi);
2053 }
2054
2055 /*
2056 * Ioctl pseudo strategy routine
2057 *
2058 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
2059 * happens here is:
2060 *
2061 * - wdioctl() queues a wd_ioctl structure.
2062 *
2063 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
2064 * user space I/O is required. If physio() is called, physio() eventually
2065 * calls wdioctlstrategy().
2066 *
2067 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
2068 * to perform the actual command
2069 *
2070 * The reason for the use of the pseudo strategy routine is because
2071 * when doing I/O to/from user space, physio _really_ wants to be in
2072 * the loop. We could put the entire buffer into the ioctl request
2073 * structure, but that won't scale if we want to do things like download
2074 * microcode.
2075 */
2076
2077 void
2078 wdioctlstrategy(struct buf *bp)
2079 {
2080 struct wd_ioctl *wi;
2081 struct ata_command ata_c;
2082 int error = 0;
2083
2084 wi = wi_find(bp);
2085 if (wi == NULL) {
2086 printf("wdioctlstrategy: "
2087 "No matching ioctl request found in queue\n");
2088 error = EINVAL;
2089 goto bad;
2090 }
2091
2092 memset(&ata_c, 0, sizeof(ata_c));
2093
2094 /*
2095 * Abort if physio broke up the transfer
2096 */
2097
2098 if (bp->b_bcount != wi->wi_atareq.datalen) {
2099 printf("physio split wd ioctl request... cannot proceed\n");
2100 error = EIO;
2101 goto bad;
2102 }
2103
2104 /*
2105 * Abort if we didn't get a buffer size that was a multiple of
2106 * our sector size (or was larger than NBBY)
2107 */
2108
2109 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
2110 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
2111 (1 << NBBY)) {
2112 error = EINVAL;
2113 goto bad;
2114 }
2115
2116 /*
2117 * Make sure a timeout was supplied in the ioctl request
2118 */
2119
2120 if (wi->wi_atareq.timeout == 0) {
2121 error = EINVAL;
2122 goto bad;
2123 }
2124
2125 if (wi->wi_atareq.flags & ATACMD_READ)
2126 ata_c.flags |= AT_READ;
2127 else if (wi->wi_atareq.flags & ATACMD_WRITE)
2128 ata_c.flags |= AT_WRITE;
2129
2130 if (wi->wi_atareq.flags & ATACMD_READREG)
2131 ata_c.flags |= AT_READREG;
2132
2133 if ((wi->wi_atareq.flags & ATACMD_LBA) != 0)
2134 ata_c.flags |= AT_LBA;
2135
2136 ata_c.flags |= AT_WAIT;
2137
2138 ata_c.timeout = wi->wi_atareq.timeout;
2139 ata_c.r_command = wi->wi_atareq.command;
2140 ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) |
2141 (wi->wi_atareq.cylinder << 8) |
2142 wi->wi_atareq.sec_num;
2143 ata_c.r_count = wi->wi_atareq.sec_count;
2144 ata_c.r_features = wi->wi_atareq.features;
2145 ata_c.r_st_bmask = WDCS_DRDY;
2146 ata_c.r_st_pmask = WDCS_DRDY;
2147 ata_c.data = wi->wi_bp.b_data;
2148 ata_c.bcount = wi->wi_bp.b_bcount;
2149
2150 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
2151 != ATACMD_COMPLETE) {
2152 wi->wi_atareq.retsts = ATACMD_ERROR;
2153 goto bad;
2154 }
2155
2156 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2157 if (ata_c.flags & AT_ERROR) {
2158 wi->wi_atareq.retsts = ATACMD_ERROR;
2159 wi->wi_atareq.error = ata_c.r_error;
2160 } else if (ata_c.flags & AT_DF)
2161 wi->wi_atareq.retsts = ATACMD_DF;
2162 else
2163 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
2164 } else {
2165 wi->wi_atareq.retsts = ATACMD_OK;
2166 if (wi->wi_atareq.flags & ATACMD_READREG) {
2167 wi->wi_atareq.command = ata_c.r_status;
2168 wi->wi_atareq.features = ata_c.r_error;
2169 wi->wi_atareq.sec_count = ata_c.r_count;
2170 wi->wi_atareq.sec_num = ata_c.r_lba & 0xff;
2171 wi->wi_atareq.head = (ata_c.r_device & 0xf0) |
2172 ((ata_c.r_lba >> 24) & 0x0f);
2173 wi->wi_atareq.cylinder = (ata_c.r_lba >> 8) & 0xffff;
2174 wi->wi_atareq.error = ata_c.r_error;
2175 }
2176 }
2177
2178 bp->b_error = 0;
2179 biodone(bp);
2180 return;
2181 bad:
2182 bp->b_error = error;
2183 bp->b_resid = bp->b_bcount;
2184 biodone(bp);
2185 }
2186