wd.c revision 1.412 1 /* $NetBSD: wd.c,v 1.412 2014/08/10 16:44:35 tls 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 2014/08/10 16:44:35 tls 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
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 =
1086 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1087 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1088 lp->d_npartitions = RAW_PART + 1;
1089
1090 lp->d_magic = DISKMAGIC;
1091 lp->d_magic2 = DISKMAGIC;
1092 lp->d_checksum = dkcksum(lp);
1093 }
1094
1095 /*
1096 * Fabricate a default disk label, and try to read the correct one.
1097 */
1098 void
1099 wdgetdisklabel(struct wd_softc *wd)
1100 {
1101 struct disklabel *lp = wd->sc_dk.dk_label;
1102 const char *errstring;
1103 int s;
1104
1105 ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1106
1107 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1108
1109 wdgetdefaultlabel(wd, lp);
1110
1111 wd->sc_badsect[0] = -1;
1112
1113 if (wd->drvp->state > RESET) {
1114 s = splbio();
1115 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1116 splx(s);
1117 }
1118 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1119 RAW_PART), wdstrategy, lp,
1120 wd->sc_dk.dk_cpulabel);
1121 if (errstring) {
1122 /*
1123 * This probably happened because the drive's default
1124 * geometry doesn't match the DOS geometry. We
1125 * assume the DOS geometry is now in the label and try
1126 * again. XXX This is a kluge.
1127 */
1128 if (wd->drvp->state > RESET) {
1129 s = splbio();
1130 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1131 splx(s);
1132 }
1133 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1134 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1135 }
1136 if (errstring) {
1137 aprint_error_dev(wd->sc_dev, "%s\n", errstring);
1138 return;
1139 }
1140
1141 if (wd->drvp->state > RESET) {
1142 s = splbio();
1143 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1144 splx(s);
1145 }
1146 #ifdef HAS_BAD144_HANDLING
1147 if ((lp->d_flags & D_BADSECT) != 0)
1148 bad144intern(wd);
1149 #endif
1150 }
1151
1152 void
1153 wdperror(const struct wd_softc *wd)
1154 {
1155 static const char *const errstr0_3[] = {"address mark not found",
1156 "track 0 not found", "aborted command", "media change requested",
1157 "id not found", "media changed", "uncorrectable data error",
1158 "bad block detected"};
1159 static const char *const errstr4_5[] = {
1160 "obsolete (address mark not found)",
1161 "no media/write protected", "aborted command",
1162 "media change requested", "id not found", "media changed",
1163 "uncorrectable data error", "interface CRC error"};
1164 const char *const *errstr;
1165 int i;
1166 const char *sep = "";
1167
1168 const char *devname = device_xname(wd->sc_dev);
1169 struct ata_drive_datas *drvp = wd->drvp;
1170 int errno = wd->sc_wdc_bio.r_error;
1171
1172 if (drvp->ata_vers >= 4)
1173 errstr = errstr4_5;
1174 else
1175 errstr = errstr0_3;
1176
1177 printf("%s: (", devname);
1178
1179 if (errno == 0)
1180 printf("error not notified");
1181
1182 for (i = 0; i < 8; i++) {
1183 if (errno & (1 << i)) {
1184 printf("%s%s", sep, errstr[i]);
1185 sep = ", ";
1186 }
1187 }
1188 printf(")\n");
1189 }
1190
1191 int
1192 wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
1193 {
1194 struct wd_softc *wd =
1195 device_lookup_private(&wd_cd, WDUNIT(dev));
1196 int error, s;
1197 #ifdef __HAVE_OLD_DISKLABEL
1198 struct disklabel *newlabel = NULL;
1199 #endif
1200
1201 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1202
1203 if ((wd->sc_flags & WDF_LOADED) == 0)
1204 return EIO;
1205
1206 error = disk_ioctl(&wd->sc_dk, xfer, addr, flag, l);
1207 if (error != EPASSTHROUGH)
1208 return (error);
1209
1210 error = 0;
1211 switch (xfer) {
1212 #ifdef HAS_BAD144_HANDLING
1213 case DIOCSBAD:
1214 if ((flag & FWRITE) == 0)
1215 return EBADF;
1216 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1217 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1218 bad144intern(wd);
1219 return 0;
1220 #endif
1221 #ifdef WD_SOFTBADSECT
1222 case DIOCBSLIST :
1223 {
1224 u_int32_t count, missing, skip;
1225 struct disk_badsecinfo dbsi;
1226 struct disk_badsectors *dbs;
1227 size_t available;
1228 uint8_t *laddr;
1229
1230 dbsi = *(struct disk_badsecinfo *)addr;
1231 missing = wd->sc_bscount;
1232 count = 0;
1233 available = dbsi.dbsi_bufsize;
1234 skip = dbsi.dbsi_skip;
1235 laddr = (uint8_t *)dbsi.dbsi_buffer;
1236
1237 /*
1238 * We start this loop with the expectation that all of the
1239 * entries will be missed and decrement this counter each
1240 * time we either skip over one (already copied out) or
1241 * we actually copy it back to user space. The structs
1242 * holding the bad sector information are copied directly
1243 * back to user space whilst the summary is returned via
1244 * the struct passed in via the ioctl.
1245 */
1246 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1247 if (skip > 0) {
1248 missing--;
1249 skip--;
1250 continue;
1251 }
1252 if (available < sizeof(*dbs))
1253 break;
1254 available -= sizeof(*dbs);
1255 copyout(dbs, laddr, sizeof(*dbs));
1256 laddr += sizeof(*dbs);
1257 missing--;
1258 count++;
1259 }
1260 dbsi.dbsi_left = missing;
1261 dbsi.dbsi_copied = count;
1262 *(struct disk_badsecinfo *)addr = dbsi;
1263 return 0;
1264 }
1265
1266 case DIOCBSFLUSH :
1267 /* Clean out the bad sector list */
1268 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1269 void *head = SLIST_FIRST(&wd->sc_bslist);
1270 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1271 free(head, M_TEMP);
1272 }
1273 wd->sc_bscount = 0;
1274 return 0;
1275 #endif
1276 case DIOCGDINFO:
1277 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1278 return 0;
1279 #ifdef __HAVE_OLD_DISKLABEL
1280 case ODIOCGDINFO:
1281 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1282 if (newlabel == NULL)
1283 return EIO;
1284 *newlabel = *(wd->sc_dk.dk_label);
1285 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1286 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1287 else
1288 error = ENOTTY;
1289 free(newlabel, M_TEMP);
1290 return error;
1291 #endif
1292
1293 case DIOCGPART:
1294 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1295 ((struct partinfo *)addr)->part =
1296 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1297 return 0;
1298
1299 case DIOCWDINFO:
1300 case DIOCSDINFO:
1301 #ifdef __HAVE_OLD_DISKLABEL
1302 case ODIOCWDINFO:
1303 case ODIOCSDINFO:
1304 #endif
1305 {
1306 struct disklabel *lp;
1307
1308 if ((flag & FWRITE) == 0)
1309 return EBADF;
1310
1311 #ifdef __HAVE_OLD_DISKLABEL
1312 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1313 newlabel = malloc(sizeof *newlabel, M_TEMP,
1314 M_WAITOK | M_ZERO);
1315 if (newlabel == NULL)
1316 return EIO;
1317 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1318 lp = newlabel;
1319 } else
1320 #endif
1321 lp = (struct disklabel *)addr;
1322
1323 mutex_enter(&wd->sc_dk.dk_openlock);
1324 wd->sc_flags |= WDF_LABELLING;
1325
1326 error = setdisklabel(wd->sc_dk.dk_label,
1327 lp, /*wd->sc_dk.dk_openmask : */0,
1328 wd->sc_dk.dk_cpulabel);
1329 if (error == 0) {
1330 if (wd->drvp->state > RESET) {
1331 s = splbio();
1332 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1333 splx(s);
1334 }
1335 if (xfer == DIOCWDINFO
1336 #ifdef __HAVE_OLD_DISKLABEL
1337 || xfer == ODIOCWDINFO
1338 #endif
1339 )
1340 error = writedisklabel(WDLABELDEV(dev),
1341 wdstrategy, wd->sc_dk.dk_label,
1342 wd->sc_dk.dk_cpulabel);
1343 }
1344
1345 wd->sc_flags &= ~WDF_LABELLING;
1346 mutex_exit(&wd->sc_dk.dk_openlock);
1347 #ifdef __HAVE_OLD_DISKLABEL
1348 if (newlabel != NULL)
1349 free(newlabel, M_TEMP);
1350 #endif
1351 return error;
1352 }
1353
1354 case DIOCKLABEL:
1355 if (*(int *)addr)
1356 wd->sc_flags |= WDF_KLABEL;
1357 else
1358 wd->sc_flags &= ~WDF_KLABEL;
1359 return 0;
1360
1361 case DIOCWLABEL:
1362 if ((flag & FWRITE) == 0)
1363 return EBADF;
1364 if (*(int *)addr)
1365 wd->sc_flags |= WDF_WLABEL;
1366 else
1367 wd->sc_flags &= ~WDF_WLABEL;
1368 return 0;
1369
1370 case DIOCGDEFLABEL:
1371 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1372 return 0;
1373 #ifdef __HAVE_OLD_DISKLABEL
1374 case ODIOCGDEFLABEL:
1375 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1376 if (newlabel == NULL)
1377 return EIO;
1378 wdgetdefaultlabel(wd, newlabel);
1379 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1380 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1381 else
1382 error = ENOTTY;
1383 free(newlabel, M_TEMP);
1384 return error;
1385 #endif
1386
1387 #ifdef notyet
1388 case DIOCWFORMAT:
1389 if ((flag & FWRITE) == 0)
1390 return EBADF;
1391 {
1392 register struct format_op *fop;
1393 struct iovec aiov;
1394 struct uio auio;
1395
1396 fop = (struct format_op *)addr;
1397 aiov.iov_base = fop->df_buf;
1398 aiov.iov_len = fop->df_count;
1399 auio.uio_iov = &aiov;
1400 auio.uio_iovcnt = 1;
1401 auio.uio_resid = fop->df_count;
1402 auio.uio_offset =
1403 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1404 auio.uio_vmspace = l->l_proc->p_vmspace;
1405 error = physio(wdformat, NULL, dev, B_WRITE, wdminphys,
1406 &auio);
1407 fop->df_count -= auio.uio_resid;
1408 fop->df_reg[0] = wdc->sc_status;
1409 fop->df_reg[1] = wdc->sc_error;
1410 return error;
1411 }
1412 #endif
1413 case DIOCGCACHE:
1414 return wd_getcache(wd, (int *)addr);
1415
1416 case DIOCSCACHE:
1417 return wd_setcache(wd, *(int *)addr);
1418
1419 case DIOCCACHESYNC:
1420 return wd_flushcache(wd, AT_WAIT);
1421
1422 case ATAIOCCOMMAND:
1423 /*
1424 * Make sure this command is (relatively) safe first
1425 */
1426 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1427 (flag & FWRITE) == 0)
1428 return (EBADF);
1429 {
1430 struct wd_ioctl *wi;
1431 atareq_t *atareq = (atareq_t *) addr;
1432 int error1;
1433
1434 wi = wi_get();
1435 wi->wi_softc = wd;
1436 wi->wi_atareq = *atareq;
1437
1438 if (atareq->datalen && atareq->flags &
1439 (ATACMD_READ | ATACMD_WRITE)) {
1440 void *tbuf;
1441 if (atareq->datalen < DEV_BSIZE
1442 && atareq->command == WDCC_IDENTIFY) {
1443 tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1444 wi->wi_iov.iov_base = tbuf;
1445 wi->wi_iov.iov_len = DEV_BSIZE;
1446 UIO_SETUP_SYSSPACE(&wi->wi_uio);
1447 } else {
1448 tbuf = NULL;
1449 wi->wi_iov.iov_base = atareq->databuf;
1450 wi->wi_iov.iov_len = atareq->datalen;
1451 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1452 }
1453 wi->wi_uio.uio_iov = &wi->wi_iov;
1454 wi->wi_uio.uio_iovcnt = 1;
1455 wi->wi_uio.uio_resid = atareq->datalen;
1456 wi->wi_uio.uio_offset = 0;
1457 wi->wi_uio.uio_rw =
1458 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1459 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1460 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1461 wdminphys, &wi->wi_uio);
1462 if (tbuf != NULL && error1 == 0) {
1463 error1 = copyout(tbuf, atareq->databuf,
1464 atareq->datalen);
1465 free(tbuf, M_TEMP);
1466 }
1467 } else {
1468 /* No need to call physio if we don't have any
1469 user data */
1470 wi->wi_bp.b_flags = 0;
1471 wi->wi_bp.b_data = 0;
1472 wi->wi_bp.b_bcount = 0;
1473 wi->wi_bp.b_dev = 0;
1474 wi->wi_bp.b_proc = l->l_proc;
1475 wdioctlstrategy(&wi->wi_bp);
1476 error1 = wi->wi_bp.b_error;
1477 }
1478 *atareq = wi->wi_atareq;
1479 wi_free(wi);
1480 return(error1);
1481 }
1482
1483 case DIOCAWEDGE:
1484 {
1485 struct dkwedge_info *dkw = (void *) addr;
1486
1487 if ((flag & FWRITE) == 0)
1488 return (EBADF);
1489
1490 /* If the ioctl happens here, the parent is us. */
1491 strcpy(dkw->dkw_parent, device_xname(wd->sc_dev));
1492 return (dkwedge_add(dkw));
1493 }
1494
1495 case DIOCDWEDGE:
1496 {
1497 struct dkwedge_info *dkw = (void *) addr;
1498
1499 if ((flag & FWRITE) == 0)
1500 return (EBADF);
1501
1502 /* If the ioctl happens here, the parent is us. */
1503 strcpy(dkw->dkw_parent, device_xname(wd->sc_dev));
1504 return (dkwedge_del(dkw));
1505 }
1506
1507 case DIOCLWEDGES:
1508 {
1509 struct dkwedge_list *dkwl = (void *) addr;
1510
1511 return (dkwedge_list(&wd->sc_dk, dkwl, l));
1512 }
1513
1514 case DIOCGSTRATEGY:
1515 {
1516 struct disk_strategy *dks = (void *)addr;
1517
1518 s = splbio();
1519 strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q),
1520 sizeof(dks->dks_name));
1521 splx(s);
1522 dks->dks_paramlen = 0;
1523
1524 return 0;
1525 }
1526
1527 case DIOCSSTRATEGY:
1528 {
1529 struct disk_strategy *dks = (void *)addr;
1530 struct bufq_state *new;
1531 struct bufq_state *old;
1532
1533 if ((flag & FWRITE) == 0) {
1534 return EBADF;
1535 }
1536 if (dks->dks_param != NULL) {
1537 return EINVAL;
1538 }
1539 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1540 error = bufq_alloc(&new, dks->dks_name,
1541 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1542 if (error) {
1543 return error;
1544 }
1545 s = splbio();
1546 old = wd->sc_q;
1547 bufq_move(new, old);
1548 wd->sc_q = new;
1549 splx(s);
1550 bufq_free(old);
1551
1552 return 0;
1553 }
1554
1555 default:
1556 return ENOTTY;
1557 }
1558
1559 #ifdef DIAGNOSTIC
1560 panic("wdioctl: impossible");
1561 #endif
1562 }
1563
1564 static int
1565 wddiscard(dev_t dev, off_t pos, off_t len)
1566 {
1567 struct wd_softc *wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1568 daddr_t bno;
1569 long size, done;
1570 long maxatonce, amount;
1571 int result;
1572
1573 if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7)
1574 || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) {
1575 /* not supported; ignore request */
1576 ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS);
1577 return 0;
1578 }
1579 maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/
1580
1581 ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS);
1582
1583 if ((wd->sc_flags & WDF_LOADED) == 0)
1584 return EIO;
1585
1586 /* round the start up and the end down */
1587 bno = (pos + DEV_BSIZE - 1) >> DEV_BSHIFT;
1588 size = ((pos + len) >> DEV_BSHIFT) - bno;
1589
1590 done = 0;
1591 while (done < size) {
1592 amount = size - done;
1593 if (amount > maxatonce) {
1594 amount = maxatonce;
1595 }
1596 result = wd_trim(wd, WDPART(dev), bno + done, amount);
1597 if (result) {
1598 return result;
1599 }
1600 done += amount;
1601 }
1602 return 0;
1603 }
1604
1605 #ifdef B_FORMAT
1606 int
1607 wdformat(struct buf *bp)
1608 {
1609
1610 bp->b_flags |= B_FORMAT;
1611 return wdstrategy(bp);
1612 }
1613 #endif
1614
1615 int
1616 wdsize(dev_t dev)
1617 {
1618 struct wd_softc *wd;
1619 int part, omask;
1620 int size;
1621
1622 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1623
1624 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1625 if (wd == NULL)
1626 return (-1);
1627
1628 part = WDPART(dev);
1629 omask = wd->sc_dk.dk_openmask & (1 << part);
1630
1631 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1632 return (-1);
1633 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1634 size = -1;
1635 else
1636 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1637 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1638 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1639 return (-1);
1640 return (size);
1641 }
1642
1643 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1644 static int wddoingadump = 0;
1645 static int wddumprecalibrated = 0;
1646
1647 /*
1648 * Dump core after a system crash.
1649 */
1650 int
1651 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1652 {
1653 struct wd_softc *wd; /* disk unit to do the I/O */
1654 struct disklabel *lp; /* disk's disklabel */
1655 int part, err;
1656 int nblks; /* total number of sectors left to write */
1657
1658 /* Check if recursive dump; if so, punt. */
1659 if (wddoingadump)
1660 return EFAULT;
1661 wddoingadump = 1;
1662
1663 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1664 if (wd == NULL)
1665 return (ENXIO);
1666
1667 part = WDPART(dev);
1668
1669 /* Convert to disk sectors. Request must be a multiple of size. */
1670 lp = wd->sc_dk.dk_label;
1671 if ((size % lp->d_secsize) != 0)
1672 return EFAULT;
1673 nblks = size / lp->d_secsize;
1674 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1675
1676 /* Check transfer bounds against partition size. */
1677 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1678 return EINVAL;
1679
1680 /* Offset block number to start of partition. */
1681 blkno += lp->d_partitions[part].p_offset;
1682
1683 /* Recalibrate, if first dump transfer. */
1684 if (wddumprecalibrated == 0) {
1685 wddumprecalibrated = 1;
1686 (*wd->atabus->ata_reset_drive)(wd->drvp,
1687 AT_POLL | AT_RST_EMERG, NULL);
1688 wd->drvp->state = RESET;
1689 }
1690
1691 wd->sc_bp = NULL;
1692 wd->sc_wdc_bio.blkno = blkno;
1693 wd->sc_wdc_bio.flags = ATA_POLL;
1694 if (wd->sc_flags & WDF_LBA48 &&
1695 (wd->sc_wdc_bio.blkno + nblks) > wd->sc_capacity28)
1696 wd->sc_wdc_bio.flags |= ATA_LBA48;
1697 if (wd->sc_flags & WDF_LBA)
1698 wd->sc_wdc_bio.flags |= ATA_LBA;
1699 wd->sc_wdc_bio.bcount = nblks * lp->d_secsize;
1700 wd->sc_wdc_bio.databuf = va;
1701 #ifndef WD_DUMP_NOT_TRUSTED
1702 switch (err = wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1703 case ATACMD_TRY_AGAIN:
1704 panic("wddump: try again");
1705 break;
1706 case ATACMD_QUEUED:
1707 panic("wddump: polled command has been queued");
1708 break;
1709 case ATACMD_COMPLETE:
1710 break;
1711 default:
1712 panic("wddump: unknown atacmd code %d", err);
1713 }
1714 switch(err = wd->sc_wdc_bio.error) {
1715 case TIMEOUT:
1716 printf("wddump: device timed out");
1717 err = EIO;
1718 break;
1719 case ERR_DF:
1720 printf("wddump: drive fault");
1721 err = EIO;
1722 break;
1723 case ERR_DMA:
1724 printf("wddump: DMA error");
1725 err = EIO;
1726 break;
1727 case ERROR:
1728 printf("wddump: ");
1729 wdperror(wd);
1730 err = EIO;
1731 break;
1732 case NOERROR:
1733 err = 0;
1734 break;
1735 default:
1736 panic("wddump: unknown error type %d", err);
1737 }
1738 if (err != 0) {
1739 printf("\n");
1740 return err;
1741 }
1742 #else /* WD_DUMP_NOT_TRUSTED */
1743 /* Let's just talk about this first... */
1744 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1745 unit, va, cylin, head, sector);
1746 delay(500 * 1000); /* half a second */
1747 #endif
1748
1749 wddoingadump = 0;
1750 return 0;
1751 }
1752
1753 #ifdef HAS_BAD144_HANDLING
1754 /*
1755 * Internalize the bad sector table.
1756 */
1757 void
1758 bad144intern(struct wd_softc *wd)
1759 {
1760 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1761 struct disklabel *lp = wd->sc_dk.dk_label;
1762 int i = 0;
1763
1764 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1765
1766 for (; i < NBT_BAD; i++) {
1767 if (bt->bt_bad[i].bt_cyl == 0xffff)
1768 break;
1769 wd->sc_badsect[i] =
1770 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1771 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1772 (bt->bt_bad[i].bt_trksec & 0xff);
1773 }
1774 for (; i < NBT_BAD+1; i++)
1775 wd->sc_badsect[i] = -1;
1776 }
1777 #endif
1778
1779 static void
1780 wd_params_to_properties(struct wd_softc *wd, struct ataparams *params)
1781 {
1782 struct disk_geom *dg = &wd->sc_dk.dk_geom;
1783
1784 memset(dg, 0, sizeof(*dg));
1785
1786 dg->dg_secperunit = wd->sc_capacity;
1787 dg->dg_secsize = DEV_BSIZE /* XXX 512? */;
1788 dg->dg_nsectors = wd->sc_params.atap_sectors;
1789 dg->dg_ntracks = wd->sc_params.atap_heads;
1790 if ((wd->sc_flags & WDF_LBA) == 0)
1791 dg->dg_ncylinders = wd->sc_params.atap_cylinders;
1792
1793 /* XXX Should have a case for ATA here, too. */
1794 const char *cp = strcmp(wd->sc_params.atap_model, "ST506") ?
1795 "ST506" : "ESDI";
1796
1797 disk_set_info(wd->sc_dev, &wd->sc_dk, cp);
1798 }
1799
1800 int
1801 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
1802 {
1803
1804 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1805 case CMD_AGAIN:
1806 return 1;
1807 case CMD_ERR:
1808 if (wd->drvp->drive_type != ATA_DRIVET_OLD)
1809 return 1;
1810 /*
1811 * We `know' there's a drive here; just assume it's old.
1812 * This geometry is only used to read the MBR and print a
1813 * (false) attach message.
1814 */
1815 strncpy(params->atap_model, "ST506",
1816 sizeof params->atap_model);
1817 params->atap_config = ATA_CFG_FIXED;
1818 params->atap_cylinders = 1024;
1819 params->atap_heads = 8;
1820 params->atap_sectors = 17;
1821 params->atap_multi = 1;
1822 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1823 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1824 /* FALLTHROUGH */
1825 case CMD_OK:
1826 wd_params_to_properties(wd, params);
1827 return 0;
1828 default:
1829 panic("wd_get_params: bad return code from ata_get_params");
1830 /* NOTREACHED */
1831 }
1832 }
1833
1834 int
1835 wd_getcache(struct wd_softc *wd, int *bitsp)
1836 {
1837 struct ataparams params;
1838
1839 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1840 return EIO;
1841 if (params.atap_cmd_set1 == 0x0000 ||
1842 params.atap_cmd_set1 == 0xffff ||
1843 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1844 *bitsp = 0;
1845 return 0;
1846 }
1847 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1848 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1849 *bitsp |= DKCACHE_WRITE;
1850
1851 return 0;
1852 }
1853
1854 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1855
1856 int
1857 wd_setcache(struct wd_softc *wd, int bits)
1858 {
1859 struct ataparams params;
1860 struct ata_command ata_c;
1861
1862 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1863 return EIO;
1864
1865 if (params.atap_cmd_set1 == 0x0000 ||
1866 params.atap_cmd_set1 == 0xffff ||
1867 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1868 return EOPNOTSUPP;
1869
1870 if ((bits & DKCACHE_READ) == 0 ||
1871 (bits & DKCACHE_SAVE) != 0)
1872 return EOPNOTSUPP;
1873
1874 memset(&ata_c, 0, sizeof(struct ata_command));
1875 ata_c.r_command = SET_FEATURES;
1876 ata_c.r_st_bmask = 0;
1877 ata_c.r_st_pmask = 0;
1878 ata_c.timeout = 30000; /* 30s timeout */
1879 ata_c.flags = AT_WAIT;
1880 if (bits & DKCACHE_WRITE)
1881 ata_c.r_features = WDSF_WRITE_CACHE_EN;
1882 else
1883 ata_c.r_features = WDSF_WRITE_CACHE_DS;
1884 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1885 aprint_error_dev(wd->sc_dev,
1886 "wd_setcache command not complete\n");
1887 return EIO;
1888 }
1889 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1890 char sbuf[sizeof(at_errbits) + 64];
1891 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1892 aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n", sbuf);
1893 return EIO;
1894 }
1895 return 0;
1896 }
1897
1898 static int
1899 wd_standby(struct wd_softc *wd, int flags)
1900 {
1901 struct ata_command ata_c;
1902
1903 memset(&ata_c, 0, sizeof(struct ata_command));
1904 ata_c.r_command = WDCC_STANDBY_IMMED;
1905 ata_c.r_st_bmask = WDCS_DRDY;
1906 ata_c.r_st_pmask = WDCS_DRDY;
1907 ata_c.flags = flags;
1908 ata_c.timeout = 30000; /* 30s timeout */
1909 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1910 aprint_error_dev(wd->sc_dev,
1911 "standby immediate command didn't complete\n");
1912 return EIO;
1913 }
1914 if (ata_c.flags & AT_ERROR) {
1915 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1916 return ENODEV;
1917 }
1918 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1919 char sbuf[sizeof(at_errbits) + 64];
1920 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1921 aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n", sbuf);
1922 return EIO;
1923 }
1924 return 0;
1925 }
1926
1927 int
1928 wd_flushcache(struct wd_softc *wd, int flags)
1929 {
1930 struct ata_command ata_c;
1931
1932 /*
1933 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1934 * only ATA-2 and still support it.
1935 */
1936 if (wd->drvp->ata_vers < 4 &&
1937 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1938 wd->sc_params.atap_cmd_set2 == 0xffff))
1939 return ENODEV;
1940 memset(&ata_c, 0, sizeof(struct ata_command));
1941 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1942 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) {
1943 ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1944 flags |= AT_LBA48;
1945 } else
1946 ata_c.r_command = WDCC_FLUSHCACHE;
1947 ata_c.r_st_bmask = WDCS_DRDY;
1948 ata_c.r_st_pmask = WDCS_DRDY;
1949 ata_c.flags = flags | AT_READREG;
1950 ata_c.timeout = 300000; /* 5m timeout */
1951 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1952 aprint_error_dev(wd->sc_dev,
1953 "flush cache command didn't complete\n");
1954 return EIO;
1955 }
1956 if (ata_c.flags & AT_ERROR) {
1957 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1958 return ENODEV;
1959 }
1960 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1961 char sbuf[sizeof(at_errbits) + 64];
1962 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
1963 aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n",
1964 sbuf);
1965 return EIO;
1966 }
1967 return 0;
1968 }
1969
1970 int
1971 wd_trim(struct wd_softc *wd, int part, daddr_t bno, long size)
1972 {
1973 struct ata_command ata_c;
1974 unsigned char *req;
1975
1976 if (part != RAW_PART)
1977 bno += wd->sc_dk.dk_label->d_partitions[part].p_offset;;
1978
1979 req = kmem_zalloc(512, KM_SLEEP);
1980 req[0] = bno & 0xff;
1981 req[1] = (bno >> 8) & 0xff;
1982 req[2] = (bno >> 16) & 0xff;
1983 req[3] = (bno >> 24) & 0xff;
1984 req[4] = (bno >> 32) & 0xff;
1985 req[5] = (bno >> 40) & 0xff;
1986 req[6] = size & 0xff;
1987 req[7] = (size >> 8) & 0xff;
1988
1989 memset(&ata_c, 0, sizeof(struct ata_command));
1990 ata_c.r_command = ATA_DATA_SET_MANAGEMENT;
1991 ata_c.r_count = 1;
1992 ata_c.r_features = ATA_SUPPORT_DSM_TRIM;
1993 ata_c.r_st_bmask = WDCS_DRDY;
1994 ata_c.r_st_pmask = WDCS_DRDY;
1995 ata_c.timeout = 30000; /* 30s timeout */
1996 ata_c.data = req;
1997 ata_c.bcount = 512;
1998 ata_c.flags |= AT_WRITE | AT_WAIT;
1999 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
2000 aprint_error_dev(wd->sc_dev,
2001 "trim command didn't complete\n");
2002 kmem_free(req, 512);
2003 return EIO;
2004 }
2005 kmem_free(req, 512);
2006 if (ata_c.flags & AT_ERROR) {
2007 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
2008 return ENODEV;
2009 }
2010 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2011 char sbuf[sizeof(at_errbits) + 64];
2012 snprintb(sbuf, sizeof(sbuf), at_errbits, ata_c.flags);
2013 aprint_error_dev(wd->sc_dev, "wd_trim: status=%s\n",
2014 sbuf);
2015 return EIO;
2016 }
2017 return 0;
2018 }
2019
2020 bool
2021 wd_shutdown(device_t dev, int how)
2022 {
2023 struct wd_softc *wd = device_private(dev);
2024
2025 /* the adapter needs to be enabled */
2026 if (wd->atabus->ata_addref(wd->drvp))
2027 return true; /* no need to complain */
2028
2029 wd_flushcache(wd, AT_POLL);
2030 if ((how & RB_POWERDOWN) == RB_POWERDOWN)
2031 wd_standby(wd, AT_POLL);
2032 return true;
2033 }
2034
2035 /*
2036 * Allocate space for a ioctl queue structure. Mostly taken from
2037 * scsipi_ioctl.c
2038 */
2039 struct wd_ioctl *
2040 wi_get(void)
2041 {
2042 struct wd_ioctl *wi;
2043 int s;
2044
2045 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
2046 buf_init(&wi->wi_bp);
2047 s = splbio();
2048 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
2049 splx(s);
2050 return (wi);
2051 }
2052
2053 /*
2054 * Free an ioctl structure and remove it from our list
2055 */
2056
2057 void
2058 wi_free(struct wd_ioctl *wi)
2059 {
2060 int s;
2061
2062 s = splbio();
2063 LIST_REMOVE(wi, wi_list);
2064 splx(s);
2065 buf_destroy(&wi->wi_bp);
2066 free(wi, M_TEMP);
2067 }
2068
2069 /*
2070 * Find a wd_ioctl structure based on the struct buf.
2071 */
2072
2073 struct wd_ioctl *
2074 wi_find(struct buf *bp)
2075 {
2076 struct wd_ioctl *wi;
2077 int s;
2078
2079 s = splbio();
2080 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
2081 if (bp == &wi->wi_bp)
2082 break;
2083 splx(s);
2084 return (wi);
2085 }
2086
2087 /*
2088 * Ioctl pseudo strategy routine
2089 *
2090 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
2091 * happens here is:
2092 *
2093 * - wdioctl() queues a wd_ioctl structure.
2094 *
2095 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
2096 * user space I/O is required. If physio() is called, physio() eventually
2097 * calls wdioctlstrategy().
2098 *
2099 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
2100 * to perform the actual command
2101 *
2102 * The reason for the use of the pseudo strategy routine is because
2103 * when doing I/O to/from user space, physio _really_ wants to be in
2104 * the loop. We could put the entire buffer into the ioctl request
2105 * structure, but that won't scale if we want to do things like download
2106 * microcode.
2107 */
2108
2109 void
2110 wdioctlstrategy(struct buf *bp)
2111 {
2112 struct wd_ioctl *wi;
2113 struct ata_command ata_c;
2114 int error = 0;
2115
2116 wi = wi_find(bp);
2117 if (wi == NULL) {
2118 printf("wdioctlstrategy: "
2119 "No matching ioctl request found in queue\n");
2120 error = EINVAL;
2121 goto bad;
2122 }
2123
2124 memset(&ata_c, 0, sizeof(ata_c));
2125
2126 /*
2127 * Abort if physio broke up the transfer
2128 */
2129
2130 if (bp->b_bcount != wi->wi_atareq.datalen) {
2131 printf("physio split wd ioctl request... cannot proceed\n");
2132 error = EIO;
2133 goto bad;
2134 }
2135
2136 /*
2137 * Abort if we didn't get a buffer size that was a multiple of
2138 * our sector size (or was larger than NBBY)
2139 */
2140
2141 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
2142 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
2143 (1 << NBBY)) {
2144 error = EINVAL;
2145 goto bad;
2146 }
2147
2148 /*
2149 * Make sure a timeout was supplied in the ioctl request
2150 */
2151
2152 if (wi->wi_atareq.timeout == 0) {
2153 error = EINVAL;
2154 goto bad;
2155 }
2156
2157 if (wi->wi_atareq.flags & ATACMD_READ)
2158 ata_c.flags |= AT_READ;
2159 else if (wi->wi_atareq.flags & ATACMD_WRITE)
2160 ata_c.flags |= AT_WRITE;
2161
2162 if (wi->wi_atareq.flags & ATACMD_READREG)
2163 ata_c.flags |= AT_READREG;
2164
2165 if ((wi->wi_atareq.flags & ATACMD_LBA) != 0)
2166 ata_c.flags |= AT_LBA;
2167
2168 ata_c.flags |= AT_WAIT;
2169
2170 ata_c.timeout = wi->wi_atareq.timeout;
2171 ata_c.r_command = wi->wi_atareq.command;
2172 ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) |
2173 (wi->wi_atareq.cylinder << 8) |
2174 wi->wi_atareq.sec_num;
2175 ata_c.r_count = wi->wi_atareq.sec_count;
2176 ata_c.r_features = wi->wi_atareq.features;
2177 ata_c.r_st_bmask = WDCS_DRDY;
2178 ata_c.r_st_pmask = WDCS_DRDY;
2179 ata_c.data = wi->wi_bp.b_data;
2180 ata_c.bcount = wi->wi_bp.b_bcount;
2181
2182 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
2183 != ATACMD_COMPLETE) {
2184 wi->wi_atareq.retsts = ATACMD_ERROR;
2185 goto bad;
2186 }
2187
2188 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2189 if (ata_c.flags & AT_ERROR) {
2190 wi->wi_atareq.retsts = ATACMD_ERROR;
2191 wi->wi_atareq.error = ata_c.r_error;
2192 } else if (ata_c.flags & AT_DF)
2193 wi->wi_atareq.retsts = ATACMD_DF;
2194 else
2195 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
2196 } else {
2197 wi->wi_atareq.retsts = ATACMD_OK;
2198 if (wi->wi_atareq.flags & ATACMD_READREG) {
2199 wi->wi_atareq.command = ata_c.r_status;
2200 wi->wi_atareq.features = ata_c.r_error;
2201 wi->wi_atareq.sec_count = ata_c.r_count;
2202 wi->wi_atareq.sec_num = ata_c.r_lba & 0xff;
2203 wi->wi_atareq.head = (ata_c.r_device & 0xf0) |
2204 ((ata_c.r_lba >> 24) & 0x0f);
2205 wi->wi_atareq.cylinder = (ata_c.r_lba >> 8) & 0xffff;
2206 wi->wi_atareq.error = ata_c.r_error;
2207 }
2208 }
2209
2210 bp->b_error = 0;
2211 biodone(bp);
2212 return;
2213 bad:
2214 bp->b_error = error;
2215 bp->b_resid = bp->b_bcount;
2216 biodone(bp);
2217 }
2218