wd.c revision 1.452 1 /* $NetBSD: wd.c,v 1.452 2019/06/06 20:55:43 mlelstv 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.452 2019/06/06 20:55:43 mlelstv Exp $");
58
59 #include "opt_ata.h"
60 #include "opt_wd.h"
61
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/kernel.h>
65 #include <sys/conf.h>
66 #include <sys/file.h>
67 #include <sys/stat.h>
68 #include <sys/ioctl.h>
69 #include <sys/buf.h>
70 #include <sys/bufq.h>
71 #include <sys/uio.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/rndsource.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 for 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_FUNCS 0x08
105 #define DEBUG_PROBE 0x10
106 #define DEBUG_DETACH 0x20
107 #define DEBUG_XFERS 0x40
108 #ifdef ATADEBUG
109 #ifndef ATADEBUG_WD_MASK
110 #define ATADEBUG_WD_MASK 0x0
111 #endif
112 int wdcdebug_wd_mask = ATADEBUG_WD_MASK;
113 #define ATADEBUG_PRINT(args, level) \
114 if (wdcdebug_wd_mask & (level)) \
115 printf args
116 #else
117 #define ATADEBUG_PRINT(args, level)
118 #endif
119
120 static int wdprobe(device_t, cfdata_t, void *);
121 static void wdattach(device_t, device_t, void *);
122 static int wddetach(device_t, int);
123 static void wdperror(const struct wd_softc *, struct ata_xfer *);
124
125 static void wdminphys(struct buf *);
126
127 static int wd_firstopen(device_t, dev_t, int, int);
128 static int wd_lastclose(device_t);
129 static bool wd_suspend(device_t, const pmf_qual_t *);
130 static int wd_standby(struct wd_softc *, int);
131
132 CFATTACH_DECL3_NEW(wd, sizeof(struct wd_softc),
133 wdprobe, wdattach, wddetach, NULL, NULL, NULL, DVF_DETACH_SHUTDOWN);
134
135 extern struct cfdriver wd_cd;
136
137 static dev_type_open(wdopen);
138 static dev_type_close(wdclose);
139 static dev_type_read(wdread);
140 static dev_type_write(wdwrite);
141 static dev_type_ioctl(wdioctl);
142 static dev_type_strategy(wdstrategy);
143 static dev_type_dump(wddump);
144 static dev_type_size(wdsize);
145 static dev_type_discard(wddiscard);
146
147 const struct bdevsw wd_bdevsw = {
148 .d_open = wdopen,
149 .d_close = wdclose,
150 .d_strategy = wdstrategy,
151 .d_ioctl = wdioctl,
152 .d_dump = wddump,
153 .d_psize = wdsize,
154 .d_discard = wddiscard,
155 .d_flag = D_DISK
156 };
157
158 const struct cdevsw wd_cdevsw = {
159 .d_open = wdopen,
160 .d_close = wdclose,
161 .d_read = wdread,
162 .d_write = wdwrite,
163 .d_ioctl = wdioctl,
164 .d_stop = nostop,
165 .d_tty = notty,
166 .d_poll = nopoll,
167 .d_mmap = nommap,
168 .d_kqfilter = nokqfilter,
169 .d_discard = wddiscard,
170 .d_flag = D_DISK
171 };
172
173 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
174 static int wddoingadump = 0;
175 static int wddumprecalibrated = 0;
176
177 /*
178 * Glue necessary to hook WDCIOCCOMMAND into physio
179 */
180
181 struct wd_ioctl {
182 LIST_ENTRY(wd_ioctl) wi_list;
183 struct buf wi_bp;
184 struct uio wi_uio;
185 struct iovec wi_iov;
186 atareq_t wi_atareq;
187 struct wd_softc *wi_softc;
188 };
189
190 static struct wd_ioctl *wi_find(struct buf *);
191 static void wi_free(struct wd_ioctl *);
192 static struct wd_ioctl *wi_get(struct wd_softc *);
193 static void wdioctlstrategy(struct buf *);
194
195 static void wdrestart(void *);
196 static void wdstart1(struct wd_softc *, struct buf *, struct ata_xfer *);
197 static int wd_diskstart(device_t, struct buf *);
198 static int wd_dumpblocks(device_t, void *, daddr_t, int);
199 static void wd_iosize(device_t, int *);
200 static int wd_discard(device_t, off_t, off_t);
201 static void wdbioretry(void *);
202 static void wdbiorequeue(void *);
203 static void wddone(device_t, struct ata_xfer *);
204 static int wd_get_params(struct wd_softc *, uint8_t, struct ataparams *);
205 static void wd_set_geometry(struct wd_softc *);
206 static int wd_flushcache(struct wd_softc *, int, bool);
207 static int wd_trim(struct wd_softc *, daddr_t, long);
208 static bool wd_shutdown(device_t, int);
209
210 static int wd_getcache(struct wd_softc *, int *);
211 static int wd_setcache(struct wd_softc *, int);
212
213 static void wd_sysctl_attach(struct wd_softc *);
214 static void wd_sysctl_detach(struct wd_softc *);
215
216 struct dkdriver wddkdriver = {
217 .d_open = wdopen,
218 .d_close = wdclose,
219 .d_strategy = wdstrategy,
220 .d_minphys = wdminphys,
221 .d_diskstart = wd_diskstart,
222 .d_dumpblocks = wd_dumpblocks,
223 .d_iosize = wd_iosize,
224 .d_firstopen = wd_firstopen,
225 .d_lastclose = wd_lastclose,
226 .d_discard = wd_discard
227 };
228
229 #ifdef HAS_BAD144_HANDLING
230 static void bad144intern(struct wd_softc *);
231 #endif
232
233 #define WD_QUIRK_SPLIT_MOD15_WRITE 0x0001 /* must split certain writes */
234
235 #define WD_QUIRK_FMT "\20\1SPLIT_MOD15_WRITE\2FORCE_LBA48"
236
237 /*
238 * Quirk table for IDE drives. Put more-specific matches first, since
239 * a simple globing routine is used for matching.
240 */
241 static const struct wd_quirk {
242 const char *wdq_match; /* inquiry pattern to match */
243 int wdq_quirks; /* drive quirks */
244 } wd_quirk_table[] = {
245 /*
246 * Some Seagate S-ATA drives have a PHY which can get confused
247 * with the way data is packetized by some S-ATA controllers.
248 *
249 * The work-around is to split in two any write transfer whose
250 * sector count % 15 == 1 (assuming 512 byte sectors).
251 *
252 * XXX This is an incomplete list. There are at least a couple
253 * XXX more model numbers. If you have trouble with such transfers
254 * XXX (8K is the most common) on Seagate S-ATA drives, please
255 * XXX notify thorpej (at) NetBSD.org.
256 *
257 * The ST360015AS has not yet been confirmed to have this
258 * issue, however, it is the only other drive in the
259 * Seagate Barracuda Serial ATA V family.
260 *
261 */
262 { "ST3120023AS",
263 WD_QUIRK_SPLIT_MOD15_WRITE },
264 { "ST380023AS",
265 WD_QUIRK_SPLIT_MOD15_WRITE },
266 { "ST360015AS",
267 WD_QUIRK_SPLIT_MOD15_WRITE },
268 { NULL,
269 0 }
270 };
271
272 static const struct wd_quirk *
273 wd_lookup_quirks(const char *name)
274 {
275 const struct wd_quirk *wdq;
276 const char *estr;
277
278 for (wdq = wd_quirk_table; wdq->wdq_match != NULL; wdq++) {
279 /*
280 * We only want exact matches (which include matches
281 * against globbing characters).
282 */
283 if (pmatch(name, wdq->wdq_match, &estr) == 2)
284 return (wdq);
285 }
286 return (NULL);
287 }
288
289 static int
290 wdprobe(device_t parent, cfdata_t match, void *aux)
291 {
292 struct ata_device *adev = aux;
293
294 if (adev == NULL)
295 return 0;
296 if (adev->adev_bustype->bustype_type != SCSIPI_BUSTYPE_ATA)
297 return 0;
298
299 if (match->cf_loc[ATA_HLCF_DRIVE] != ATA_HLCF_DRIVE_DEFAULT &&
300 match->cf_loc[ATA_HLCF_DRIVE] != adev->adev_drv_data->drive)
301 return 0;
302 return 1;
303 }
304
305 static void
306 wdattach(device_t parent, device_t self, void *aux)
307 {
308 struct wd_softc *wd = device_private(self);
309 struct dk_softc *dksc = &wd->sc_dksc;
310 struct ata_device *adev= aux;
311 int i, blank;
312 char tbuf[41],pbuf[9], c, *p, *q;
313 const struct wd_quirk *wdq;
314 int dtype = DKTYPE_UNKNOWN;
315
316 dksc->sc_dev = self;
317
318 ATADEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
319 mutex_init(&wd->sc_lock, MUTEX_DEFAULT, IPL_BIO);
320 #ifdef WD_SOFTBADSECT
321 SLIST_INIT(&wd->sc_bslist);
322 #endif
323 wd->atabus = adev->adev_bustype;
324 wd->inflight = 0;
325 wd->drvp = adev->adev_drv_data;
326
327 wd->drvp->drv_openings = 1;
328 wd->drvp->drv_done = wddone;
329 wd->drvp->drv_softc = dksc->sc_dev; /* done in atabusconfig_thread()
330 but too late */
331
332 SLIST_INIT(&wd->sc_retry_list);
333 SLIST_INIT(&wd->sc_requeue_list);
334 callout_init(&wd->sc_retry_callout, 0); /* XXX MPSAFE */
335 callout_init(&wd->sc_requeue_callout, 0); /* XXX MPSAFE */
336 callout_init(&wd->sc_restart_diskqueue, 0); /* XXX MPSAFE */
337
338 aprint_naive("\n");
339 aprint_normal("\n");
340
341 /* read our drive info */
342 if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
343 aprint_error_dev(self, "IDENTIFY failed\n");
344 goto out;
345 }
346
347 for (blank = 0, p = wd->sc_params.atap_model, q = tbuf, i = 0;
348 i < sizeof(wd->sc_params.atap_model); i++) {
349 c = *p++;
350 if (c == '\0')
351 break;
352 if (c != ' ') {
353 if (blank) {
354 *q++ = ' ';
355 blank = 0;
356 }
357 *q++ = c;
358 } else
359 blank = 1;
360 }
361 *q++ = '\0';
362
363 wd->sc_typename = kmem_asprintf("%s", tbuf);
364 aprint_normal_dev(self, "<%s>\n", wd->sc_typename);
365
366 wdq = wd_lookup_quirks(tbuf);
367 if (wdq != NULL)
368 wd->sc_quirks = wdq->wdq_quirks;
369
370 if (wd->sc_quirks != 0) {
371 char sbuf[sizeof(WD_QUIRK_FMT) + 64];
372 snprintb(sbuf, sizeof(sbuf), WD_QUIRK_FMT, wd->sc_quirks);
373 aprint_normal_dev(self, "quirks %s\n", sbuf);
374
375 if (wd->sc_quirks & WD_QUIRK_SPLIT_MOD15_WRITE) {
376 aprint_error_dev(self, "drive corrupts write transfers with certain controllers, consider replacing\n");
377 }
378 }
379
380 if ((wd->sc_params.atap_multi & 0xff) > 1) {
381 wd->drvp->multi = wd->sc_params.atap_multi & 0xff;
382 } else {
383 wd->drvp->multi = 1;
384 }
385
386 aprint_verbose_dev(self, "drive supports %d-sector PIO transfers,",
387 wd->drvp->multi);
388
389 /* 48-bit LBA addressing */
390 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0)
391 wd->sc_flags |= WDF_LBA48;
392
393 /* Prior to ATA-4, LBA was optional. */
394 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
395 wd->sc_flags |= WDF_LBA;
396 #if 0
397 /* ATA-4 requires LBA. */
398 if (wd->sc_params.atap_ataversion != 0xffff &&
399 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
400 wd->sc_flags |= WDF_LBA;
401 #endif
402
403 if ((wd->sc_flags & WDF_LBA48) != 0) {
404 aprint_verbose(" LBA48 addressing\n");
405 wd->sc_capacity =
406 ((uint64_t) wd->sc_params.atap_max_lba[3] << 48) |
407 ((uint64_t) wd->sc_params.atap_max_lba[2] << 32) |
408 ((uint64_t) wd->sc_params.atap_max_lba[1] << 16) |
409 ((uint64_t) wd->sc_params.atap_max_lba[0] << 0);
410 wd->sc_capacity28 =
411 (wd->sc_params.atap_capacity[1] << 16) |
412 wd->sc_params.atap_capacity[0];
413 } else if ((wd->sc_flags & WDF_LBA) != 0) {
414 aprint_verbose(" LBA addressing\n");
415 wd->sc_capacity28 = wd->sc_capacity =
416 (wd->sc_params.atap_capacity[1] << 16) |
417 wd->sc_params.atap_capacity[0];
418 } else {
419 aprint_verbose(" chs addressing\n");
420 wd->sc_capacity28 = wd->sc_capacity =
421 wd->sc_params.atap_cylinders *
422 wd->sc_params.atap_heads *
423 wd->sc_params.atap_sectors;
424 }
425 if ((wd->sc_params.atap_secsz & ATA_SECSZ_VALID_MASK) == ATA_SECSZ_VALID
426 && ((wd->sc_params.atap_secsz & ATA_SECSZ_LLS) != 0)) {
427 wd->sc_blksize = 2ULL *
428 ((uint32_t)((wd->sc_params.atap_lls_secsz[1] << 16) |
429 wd->sc_params.atap_lls_secsz[0]));
430 } else {
431 wd->sc_blksize = 512;
432 }
433 wd->sc_capacity512 = (wd->sc_capacity * wd->sc_blksize) / DEV_BSIZE;
434 format_bytes(pbuf, sizeof(pbuf), wd->sc_capacity * wd->sc_blksize);
435 aprint_normal_dev(self, "%s, %d cyl, %d head, %d sec, "
436 "%d bytes/sect x %llu sectors\n",
437 pbuf,
438 (wd->sc_flags & WDF_LBA) ? (int)(wd->sc_capacity /
439 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors)) :
440 wd->sc_params.atap_cylinders,
441 wd->sc_params.atap_heads, wd->sc_params.atap_sectors,
442 wd->sc_blksize, (unsigned long long)wd->sc_capacity);
443
444 ATADEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
445 device_xname(self), wd->sc_params.atap_dmatiming_mimi,
446 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
447
448 if (wd->sc_blksize <= 0 || !powerof2(wd->sc_blksize) ||
449 wd->sc_blksize < DEV_BSIZE || wd->sc_blksize > MAXPHYS) {
450 aprint_normal_dev(self, "WARNING: block size %u "
451 "might not actually work\n", wd->sc_blksize);
452 }
453
454 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
455 dtype = DKTYPE_ST506;
456 else
457 dtype = DKTYPE_ESDI;
458
459 out:
460 /*
461 * Initialize and attach the disk structure.
462 */
463 dk_init(dksc, self, dtype);
464 disk_init(&dksc->sc_dkdev, dksc->sc_xname, &wddkdriver);
465
466 /* Attach dk and disk subsystems */
467 dk_attach(dksc);
468 disk_attach(&dksc->sc_dkdev);
469 wd_set_geometry(wd);
470
471 bufq_alloc(&dksc->sc_bufq, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
472
473 /* reference to label structure, used by ata code */
474 wd->drvp->lp = dksc->sc_dkdev.dk_label;
475
476 /* Discover wedges on this disk. */
477 dkwedge_discover(&dksc->sc_dkdev);
478
479 if (!pmf_device_register1(self, wd_suspend, NULL, wd_shutdown))
480 aprint_error_dev(self, "couldn't establish power handler\n");
481
482 wd_sysctl_attach(wd);
483 }
484
485 static bool
486 wd_suspend(device_t dv, const pmf_qual_t *qual)
487 {
488 struct wd_softc *sc = device_private(dv);
489
490 /* the adapter needs to be enabled */
491 if (sc->atabus->ata_addref(sc->drvp))
492 return true; /* no need to complain */
493
494 wd_flushcache(sc, AT_WAIT, false);
495 wd_standby(sc, AT_WAIT);
496
497 sc->atabus->ata_delref(sc->drvp);
498 return true;
499 }
500
501 static int
502 wddetach(device_t self, int flags)
503 {
504 struct wd_softc *wd = device_private(self);
505 struct dk_softc *dksc = &wd->sc_dksc;
506 int bmaj, cmaj, i, mn, rc;
507
508 if ((rc = disk_begindetach(&dksc->sc_dkdev, wd_lastclose, self, flags)) != 0)
509 return rc;
510
511 /* locate the major number */
512 bmaj = bdevsw_lookup_major(&wd_bdevsw);
513 cmaj = cdevsw_lookup_major(&wd_cdevsw);
514
515 /* Nuke the vnodes for any open instances. */
516 for (i = 0; i < MAXPARTITIONS; i++) {
517 mn = WDMINOR(device_unit(self), i);
518 vdevgone(bmaj, mn, mn, VBLK);
519 vdevgone(cmaj, mn, mn, VCHR);
520 }
521
522 dk_drain(dksc);
523
524 /* Kill off any pending commands. */
525 mutex_enter(&wd->sc_lock);
526 wd->atabus->ata_killpending(wd->drvp);
527
528 callout_halt(&wd->sc_retry_callout, &wd->sc_lock);
529 callout_destroy(&wd->sc_retry_callout);
530 callout_halt(&wd->sc_requeue_callout, &wd->sc_lock);
531 callout_destroy(&wd->sc_requeue_callout);
532 callout_halt(&wd->sc_restart_diskqueue, &wd->sc_lock);
533 callout_destroy(&wd->sc_restart_diskqueue);
534
535 mutex_exit(&wd->sc_lock);
536
537 bufq_free(dksc->sc_bufq);
538
539 /* Delete all of our wedges. */
540 dkwedge_delall(&dksc->sc_dkdev);
541
542 if (flags & DETACH_POWEROFF)
543 wd_standby(wd, AT_POLL);
544
545 /* Detach from the disk list. */
546 disk_detach(&dksc->sc_dkdev);
547 disk_destroy(&dksc->sc_dkdev);
548
549 dk_detach(dksc);
550
551 #ifdef WD_SOFTBADSECT
552 /* Clean out the bad sector list */
553 while (!SLIST_EMPTY(&wd->sc_bslist)) {
554 struct disk_badsectors *dbs = SLIST_FIRST(&wd->sc_bslist);
555 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
556 kmem_free(dbs, sizeof(*dbs));
557 }
558 wd->sc_bscount = 0;
559 #endif
560 if (wd->sc_typename != NULL) {
561 kmem_free(wd->sc_typename, strlen(wd->sc_typename) + 1);
562 wd->sc_typename = NULL;
563 }
564
565 pmf_device_deregister(self);
566
567 wd_sysctl_detach(wd);
568
569 mutex_destroy(&wd->sc_lock);
570
571 wd->drvp->drive_type = ATA_DRIVET_NONE; /* no drive any more here */
572 wd->drvp->drive_flags = 0;
573
574 return (0);
575 }
576
577 /*
578 * Read/write routine for a buffer. Validates the arguments and schedules the
579 * transfer. Does not wait for the transfer to complete.
580 */
581 static void
582 wdstrategy(struct buf *bp)
583 {
584 struct wd_softc *wd =
585 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
586 struct dk_softc *dksc = &wd->sc_dksc;
587
588 ATADEBUG_PRINT(("wdstrategy (%s)\n", dksc->sc_xname),
589 DEBUG_XFERS);
590
591 /* If device invalidated (e.g. media change, door open,
592 * device detachment), then error.
593 */
594 if ((wd->sc_flags & WDF_LOADED) == 0 ||
595 !device_is_enabled(dksc->sc_dev))
596 goto err;
597
598 #ifdef WD_SOFTBADSECT
599 /*
600 * If the transfer about to be attempted contains only a block that
601 * is known to be bad then return an error for the transfer without
602 * even attempting to start a transfer up under the premis that we
603 * will just end up doing more retries for a transfer that will end
604 * up failing again.
605 */
606 if (__predict_false(!SLIST_EMPTY(&wd->sc_bslist))) {
607 struct disklabel *lp = dksc->sc_dkdev.dk_label;
608 struct disk_badsectors *dbs;
609 daddr_t blkno, maxblk;
610
611 /* convert the block number to absolute */
612 if (lp->d_secsize >= DEV_BSIZE)
613 blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
614 else
615 blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
616 if (WDPART(bp->b_dev) != RAW_PART)
617 blkno += lp->d_partitions[WDPART(bp->b_dev)].p_offset;
618 maxblk = blkno + (bp->b_bcount / wd->sc_blksize) - 1;
619
620 mutex_enter(&wd->sc_lock);
621 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next)
622 if ((dbs->dbs_min <= bp->b_rawblkno &&
623 bp->b_rawblkno <= dbs->dbs_max) ||
624 (dbs->dbs_min <= maxblk && maxblk <= dbs->dbs_max)){
625 mutex_exit(&wd->sc_lock);
626 goto err;
627 }
628 mutex_exit(&wd->sc_lock);
629 }
630 #endif
631
632 dk_strategy(dksc, bp);
633 return;
634
635 err:
636 bp->b_error = EIO;
637 bp->b_resid = bp->b_bcount;
638 biodone(bp);
639 }
640
641 static void
642 wdstart1(struct wd_softc *wd, struct buf *bp, struct ata_xfer *xfer)
643 {
644 struct dk_softc *dksc = &wd->sc_dksc;
645
646 KASSERT(bp == xfer->c_bio.bp || xfer->c_bio.bp == NULL);
647 KASSERT((xfer->c_flags & (C_WAITACT|C_FREE)) == 0);
648
649 /* Reset state, so that retries don't use stale info */
650 if (__predict_false(xfer->c_retries > 0)) {
651 xfer->c_flags = 0;
652 memset(&xfer->c_bio, 0, sizeof(xfer->c_bio));
653 }
654
655 xfer->c_bio.blkno = bp->b_rawblkno;
656 xfer->c_bio.bcount = bp->b_bcount;
657 xfer->c_bio.databuf = bp->b_data;
658 xfer->c_bio.blkdone = 0;
659 xfer->c_bio.bp = bp;
660
661 #ifdef WD_CHAOS_MONKEY
662 /*
663 * Override blkno to be over device capacity to trigger error,
664 * but only if it's read, to avoid trashing disk contents should
665 * the command be clipped, or otherwise misinterpreted, by the
666 * driver or controller.
667 */
668 if (BUF_ISREAD(bp) && xfer->c_retries == 0 && wd->drv_chaos_freq > 0 &&
669 (++wd->drv_chaos_cnt % wd->drv_chaos_freq) == 0) {
670 device_printf(dksc->sc_dev, "%s: chaos xfer %"PRIxPTR"\n",
671 __func__, (intptr_t)xfer & PAGE_MASK);
672 xfer->c_bio.blkno = 7777777 + wd->sc_capacity;
673 xfer->c_flags |= C_CHAOS;
674 }
675 #endif
676
677 /*
678 * If we're retrying, retry in single-sector mode. This will give us
679 * the sector number of the problem, and will eventually allow the
680 * transfer to succeed. If FUA is requested, we can't actually
681 * do this, as ATA_SINGLE is usually executed as PIO transfer by drivers
682 * which support it, and that isn't compatible with NCQ/FUA.
683 */
684 if (xfer->c_retries >= WDIORETRIES_SINGLE &&
685 (bp->b_flags & B_MEDIA_FUA) == 0)
686 xfer->c_bio.flags = ATA_SINGLE;
687 else
688 xfer->c_bio.flags = 0;
689
690 /*
691 * request LBA48 transfers when supported by the controller
692 * and needed by transfer offset or size.
693 */
694 if (wd->sc_flags & WDF_LBA48 &&
695 (((xfer->c_bio.blkno +
696 xfer->c_bio.bcount / dksc->sc_dkdev.dk_geom.dg_secsize) >
697 wd->sc_capacity28) ||
698 ((xfer->c_bio.bcount / dksc->sc_dkdev.dk_geom.dg_secsize) > 128)))
699 xfer->c_bio.flags |= ATA_LBA48;
700
701 /*
702 * If NCQ was negotiated, always use it for the first several attempts.
703 * Since device cancels all outstanding requests on error, downgrade
704 * to non-NCQ on retry, so that the retried transfer would not cause
705 * cascade failure for the other transfers if it fails again.
706 * If FUA was requested, we can't downgrade, as that would violate
707 * the semantics - FUA would not be honored. In that case, continue
708 * retrying with NCQ.
709 */
710 if (WD_USE_NCQ(wd) && (xfer->c_retries < WDIORETRIES_SINGLE ||
711 (bp->b_flags & B_MEDIA_FUA) != 0)) {
712 xfer->c_bio.flags |= ATA_LBA48;
713 xfer->c_flags |= C_NCQ;
714
715 if (WD_USE_NCQ_PRIO(wd) &&
716 BIO_GETPRIO(bp) == BPRIO_TIMECRITICAL)
717 xfer->c_bio.flags |= ATA_PRIO_HIGH;
718 }
719
720 if (wd->sc_flags & WDF_LBA)
721 xfer->c_bio.flags |= ATA_LBA;
722 if (bp->b_flags & B_READ) {
723 xfer->c_bio.flags |= ATA_READ;
724 } else {
725 /* it's a write */
726 wd->sc_flags |= WDF_DIRTY;
727 }
728 if (bp->b_flags & B_MEDIA_FUA) {
729 /* If not using NCQ, the command WRITE DMA FUA EXT is LBA48 */
730 KASSERT((wd->sc_flags & WDF_LBA48) != 0);
731 if ((xfer->c_flags & C_NCQ) == 0)
732 xfer->c_bio.flags |= ATA_LBA48;
733
734 xfer->c_bio.flags |= ATA_FUA;
735 }
736
737 if (xfer->c_retries == 0)
738 wd->inflight++;
739 switch (wd->atabus->ata_bio(wd->drvp, xfer)) {
740 case ATACMD_TRY_AGAIN:
741 panic("wdstart1: try again");
742 break;
743 case ATACMD_QUEUED:
744 case ATACMD_COMPLETE:
745 break;
746 default:
747 panic("wdstart1: bad return code from ata_bio()");
748 }
749 }
750
751 static int
752 wd_diskstart(device_t dev, struct buf *bp)
753 {
754 struct wd_softc *wd = device_private(dev);
755 #ifdef ATADEBUG
756 struct dk_softc *dksc = &wd->sc_dksc;
757 #endif
758 struct ata_xfer *xfer;
759 struct ata_channel *chp;
760 unsigned openings;
761 int ticks;
762
763 mutex_enter(&wd->sc_lock);
764
765 chp = wd->drvp->chnl_softc;
766
767 ata_channel_lock(chp);
768 openings = ata_queue_openings(chp);
769 ata_channel_unlock(chp);
770
771 openings = uimin(openings, wd->drvp->drv_openings);
772
773 if (wd->inflight >= openings) {
774 /*
775 * pretend we run out of memory when the queue is full,
776 * so that the operation is retried after a minimal
777 * delay.
778 */
779 xfer = NULL;
780 ticks = 1;
781 } else {
782 /*
783 * If there is no available memory, retry later. This
784 * happens very rarely and only under memory pressure,
785 * so wait relatively long before retry.
786 */
787 xfer = ata_get_xfer(chp, false);
788 ticks = hz/2;
789 }
790
791 if (xfer == NULL) {
792 ATADEBUG_PRINT(("wd_diskstart %s no xfer\n",
793 dksc->sc_xname), DEBUG_XFERS);
794
795 /*
796 * The disk queue is pushed automatically when an I/O
797 * operation finishes or another one is queued. We
798 * need this extra timeout because an ATA channel
799 * might be shared by more than one disk queue and
800 * all queues need to be restarted when another slot
801 * becomes available.
802 */
803 if (!callout_pending(&wd->sc_restart_diskqueue)) {
804 callout_reset(&wd->sc_restart_diskqueue, ticks,
805 wdrestart, dev);
806 }
807
808 mutex_exit(&wd->sc_lock);
809 return EAGAIN;
810 }
811
812 wdstart1(wd, bp, xfer);
813
814 mutex_exit(&wd->sc_lock);
815
816 return 0;
817 }
818
819 /*
820 * Queue a drive for I/O.
821 */
822 static void
823 wdrestart(void *x)
824 {
825 device_t self = x;
826 struct wd_softc *wd = device_private(self);
827 struct dk_softc *dksc = &wd->sc_dksc;
828
829 ATADEBUG_PRINT(("wdstart %s\n", dksc->sc_xname),
830 DEBUG_XFERS);
831
832 if (!device_is_active(dksc->sc_dev))
833 return;
834
835 dk_start(dksc, NULL);
836 }
837
838 static void
839 wddone(device_t self, struct ata_xfer *xfer)
840 {
841 struct wd_softc *wd = device_private(self);
842 struct dk_softc *dksc = &wd->sc_dksc;
843 const char *errmsg;
844 int do_perror = 0;
845 struct buf *bp;
846
847 ATADEBUG_PRINT(("wddone %s\n", dksc->sc_xname),
848 DEBUG_XFERS);
849
850 if (__predict_false(wddoingadump)) {
851 /* just drop it to the floor */
852 ata_free_xfer(wd->drvp->chnl_softc, xfer);
853 return;
854 }
855
856 bp = xfer->c_bio.bp;
857 KASSERT(bp != NULL);
858
859 bp->b_resid = xfer->c_bio.bcount;
860 switch (xfer->c_bio.error) {
861 case ERR_DMA:
862 errmsg = "DMA error";
863 goto retry;
864 case ERR_DF:
865 errmsg = "device fault";
866 goto retry;
867 case TIMEOUT:
868 errmsg = "device timeout";
869 goto retry;
870 case REQUEUE:
871 errmsg = "requeue";
872 goto retry2;
873 case ERR_RESET:
874 errmsg = "channel reset";
875 goto retry2;
876 case ERROR:
877 /* Don't care about media change bits */
878 if (xfer->c_bio.r_error != 0 &&
879 (xfer->c_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
880 goto noerror;
881 errmsg = "error";
882 do_perror = 1;
883 retry: /* Just reset and retry. Can we do more ? */
884 if ((xfer->c_flags & C_RECOVERED) == 0) {
885 int wflags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
886 ata_channel_lock(wd->drvp->chnl_softc);
887 ata_thread_run(wd->drvp->chnl_softc, wflags,
888 ATACH_TH_DRIVE_RESET, wd->drvp->drive);
889 ata_channel_unlock(wd->drvp->chnl_softc);
890 }
891 retry2:
892 mutex_enter(&wd->sc_lock);
893
894 diskerr(bp, "wd", errmsg, LOG_PRINTF,
895 xfer->c_bio.blkdone, dksc->sc_dkdev.dk_label);
896 if (xfer->c_retries < WDIORETRIES)
897 printf(", xfer %"PRIxPTR", retry %d",
898 (intptr_t)xfer & PAGE_MASK,
899 xfer->c_retries);
900 printf("\n");
901 if (do_perror)
902 wdperror(wd, xfer);
903
904 if (xfer->c_retries < WDIORETRIES) {
905 xfer->c_retries++;
906
907 /* Rerun ASAP if just requeued */
908 if (xfer->c_bio.error == REQUEUE) {
909 SLIST_INSERT_HEAD(&wd->sc_requeue_list, xfer,
910 c_retrychain);
911 callout_reset(&wd->sc_requeue_callout,
912 1, wdbiorequeue, wd);
913 } else {
914 SLIST_INSERT_HEAD(&wd->sc_retry_list, xfer,
915 c_retrychain);
916 callout_reset(&wd->sc_retry_callout,
917 RECOVERYTIME, wdbioretry, wd);
918 }
919
920 mutex_exit(&wd->sc_lock);
921 return;
922 }
923
924 mutex_exit(&wd->sc_lock);
925
926 #ifdef WD_SOFTBADSECT
927 /*
928 * Not all errors indicate a failed block but those that do,
929 * put the block on the bad-block list for the device. Only
930 * do this for reads because the drive should do it for writes,
931 * itself, according to Manuel.
932 */
933 if ((bp->b_flags & B_READ) &&
934 ((wd->drvp->ata_vers >= 4 && xfer->c_bio.r_error & 64) ||
935 (wd->drvp->ata_vers < 4 && xfer->c_bio.r_error & 192))) {
936 struct disk_badsectors *dbs;
937
938 dbs = kmem_zalloc(sizeof *dbs, KM_NOSLEEP);
939 if (dbs == NULL) {
940 aprint_error_dev(dksc->sc_dev,
941 "failed to add bad block to list\n");
942 goto out;
943 }
944
945 dbs->dbs_min = bp->b_rawblkno;
946 dbs->dbs_max = dbs->dbs_min +
947 (bp->b_bcount /wd->sc_blksize) - 1;
948 microtime(&dbs->dbs_failedat);
949
950 mutex_enter(&wd->sc_lock);
951 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
952 wd->sc_bscount++;
953 mutex_exit(&wd->sc_lock);
954 }
955 out:
956 #endif
957 bp->b_error = EIO;
958 break;
959 case NOERROR:
960 #ifdef WD_CHAOS_MONKEY
961 /*
962 * For example Parallels AHCI emulation doesn't actually
963 * return error for the invalid I/O, so just re-run
964 * the request and do not panic.
965 */
966 if (__predict_false(xfer->c_flags & C_CHAOS)) {
967 xfer->c_bio.error = REQUEUE;
968 errmsg = "chaos noerror";
969 goto retry2;
970 }
971 #endif
972
973 noerror: if ((xfer->c_bio.flags & ATA_CORR) || xfer->c_retries > 0)
974 device_printf(dksc->sc_dev,
975 "soft error (corrected) xfer %"PRIxPTR"\n",
976 (intptr_t)xfer & PAGE_MASK);
977 break;
978 case ERR_NODEV:
979 bp->b_error = EIO;
980 break;
981 }
982 if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) {
983 /*
984 * the disk or controller sometimes report a complete
985 * xfer, when there has been an error. This is wrong,
986 * assume nothing got transfered in this case
987 */
988 bp->b_resid = bp->b_bcount;
989 }
990
991 ata_free_xfer(wd->drvp->chnl_softc, xfer);
992
993 mutex_enter(&wd->sc_lock);
994 wd->inflight--;
995 mutex_exit(&wd->sc_lock);
996 dk_done(dksc, bp);
997 dk_start(dksc, NULL);
998 }
999
1000 static void
1001 wdbioretry(void *v)
1002 {
1003 struct wd_softc *wd = v;
1004 struct ata_xfer *xfer;
1005
1006 ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname),
1007 DEBUG_XFERS);
1008
1009 mutex_enter(&wd->sc_lock);
1010 while ((xfer = SLIST_FIRST(&wd->sc_retry_list))) {
1011 SLIST_REMOVE_HEAD(&wd->sc_retry_list, c_retrychain);
1012 wdstart1(wd, xfer->c_bio.bp, xfer);
1013 }
1014 mutex_exit(&wd->sc_lock);
1015 }
1016
1017 static void
1018 wdbiorequeue(void *v)
1019 {
1020 struct wd_softc *wd = v;
1021 struct ata_xfer *xfer;
1022
1023 ATADEBUG_PRINT(("%s %s\n", __func__, wd->sc_dksc.sc_xname),
1024 DEBUG_XFERS);
1025
1026 mutex_enter(&wd->sc_lock);
1027 while ((xfer = SLIST_FIRST(&wd->sc_requeue_list))) {
1028 SLIST_REMOVE_HEAD(&wd->sc_requeue_list, c_retrychain);
1029 wdstart1(wd, xfer->c_bio.bp, xfer);
1030 }
1031 mutex_exit(&wd->sc_lock);
1032 }
1033
1034 static void
1035 wdminphys(struct buf *bp)
1036 {
1037 const struct wd_softc * const wd =
1038 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
1039 int maxsectors;
1040
1041 /*
1042 * The limit is actually 65536 for LBA48 and 256 for non-LBA48,
1043 * but that requires to set the count for the ATA command
1044 * to 0, which is somewhat error prone, so better stay safe.
1045 */
1046 if (wd->sc_flags & WDF_LBA48)
1047 maxsectors = 65535;
1048 else
1049 maxsectors = 128;
1050
1051 if (bp->b_bcount > (wd->sc_blksize * maxsectors))
1052 bp->b_bcount = (wd->sc_blksize * maxsectors);
1053
1054 minphys(bp);
1055 }
1056
1057 static void
1058 wd_iosize(device_t dev, int *count)
1059 {
1060 struct buf B;
1061 int bmaj;
1062
1063 bmaj = bdevsw_lookup_major(&wd_bdevsw);
1064 B.b_dev = MAKEWDDEV(bmaj,device_unit(dev),RAW_PART);
1065 B.b_bcount = *count;
1066
1067 wdminphys(&B);
1068
1069 *count = B.b_bcount;
1070 }
1071
1072 static int
1073 wdread(dev_t dev, struct uio *uio, int flags)
1074 {
1075
1076 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
1077 return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio));
1078 }
1079
1080 static int
1081 wdwrite(dev_t dev, struct uio *uio, int flags)
1082 {
1083
1084 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
1085 return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio));
1086 }
1087
1088 static int
1089 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
1090 {
1091 struct wd_softc *wd;
1092 struct dk_softc *dksc;
1093 int unit, part, error;
1094
1095 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
1096 unit = WDUNIT(dev);
1097 wd = device_lookup_private(&wd_cd, unit);
1098 if (wd == NULL)
1099 return (ENXIO);
1100 dksc = &wd->sc_dksc;
1101
1102 if (! device_is_active(dksc->sc_dev))
1103 return (ENODEV);
1104
1105 part = WDPART(dev);
1106
1107 if (wd->sc_capacity == 0)
1108 return (ENODEV);
1109
1110 /*
1111 * If any partition is open, but the disk has been invalidated,
1112 * disallow further opens.
1113 */
1114 if ((wd->sc_flags & (WDF_OPEN | WDF_LOADED)) == WDF_OPEN) {
1115 if (part != RAW_PART || fmt != S_IFCHR)
1116 return EIO;
1117 }
1118
1119 error = dk_open(dksc, dev, flag, fmt, l);
1120
1121 return error;
1122 }
1123
1124 /*
1125 * Serialized by caller
1126 */
1127 static int
1128 wd_firstopen(device_t self, dev_t dev, int flag, int fmt)
1129 {
1130 struct wd_softc *wd = device_private(self);
1131 struct dk_softc *dksc = &wd->sc_dksc;
1132 int error;
1133
1134 error = wd->atabus->ata_addref(wd->drvp);
1135 if (error)
1136 return error;
1137
1138 if ((wd->sc_flags & WDF_LOADED) == 0) {
1139 int param_error;
1140
1141 /* Load the physical device parameters. */
1142 param_error = wd_get_params(wd, AT_WAIT, &wd->sc_params);
1143 if (param_error != 0) {
1144 aprint_error_dev(dksc->sc_dev, "IDENTIFY failed\n");
1145 error = EIO;
1146 goto bad;
1147 }
1148 wd_set_geometry(wd);
1149 wd->sc_flags |= WDF_LOADED;
1150 }
1151
1152 wd->sc_flags |= WDF_OPEN;
1153 return 0;
1154
1155 bad:
1156 wd->atabus->ata_delref(wd->drvp);
1157 return error;
1158 }
1159
1160 /*
1161 * Caller must hold wd->sc_dk.dk_openlock.
1162 */
1163 static int
1164 wd_lastclose(device_t self)
1165 {
1166 struct wd_softc *wd = device_private(self);
1167
1168 KASSERTMSG(bufq_peek(wd->sc_dksc.sc_bufq) == NULL, "bufq not empty");
1169
1170 if (wd->sc_flags & WDF_DIRTY)
1171 wd_flushcache(wd, AT_WAIT, false);
1172
1173 wd->atabus->ata_delref(wd->drvp);
1174 wd->sc_flags &= ~WDF_OPEN;
1175
1176 return 0;
1177 }
1178
1179 static int
1180 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
1181 {
1182 struct wd_softc *wd;
1183 struct dk_softc *dksc;
1184 int unit;
1185
1186 unit = WDUNIT(dev);
1187 wd = device_lookup_private(&wd_cd, unit);
1188 dksc = &wd->sc_dksc;
1189
1190 return dk_close(dksc, dev, flag, fmt, l);
1191 }
1192
1193 void
1194 wdperror(const struct wd_softc *wd, struct ata_xfer *xfer)
1195 {
1196 static const char *const errstr0_3[] = {"address mark not found",
1197 "track 0 not found", "aborted command", "media change requested",
1198 "id not found", "media changed", "uncorrectable data error",
1199 "bad block detected"};
1200 static const char *const errstr4_5[] = {
1201 "obsolete (address mark not found)",
1202 "no media/write protected", "aborted command",
1203 "media change requested", "id not found", "media changed",
1204 "uncorrectable data error", "interface CRC error"};
1205 const char *const *errstr;
1206 int i;
1207 const char *sep = "";
1208
1209 const struct dk_softc *dksc = &wd->sc_dksc;
1210 const char *devname = dksc->sc_xname;
1211 struct ata_drive_datas *drvp = wd->drvp;
1212 int errno = xfer->c_bio.r_error;
1213
1214 if (drvp->ata_vers >= 4)
1215 errstr = errstr4_5;
1216 else
1217 errstr = errstr0_3;
1218
1219 printf("%s: (", devname);
1220
1221 if (errno == 0)
1222 printf("error not notified");
1223
1224 for (i = 0; i < 8; i++) {
1225 if (errno & (1 << i)) {
1226 printf("%s%s", sep, errstr[i]);
1227 sep = ", ";
1228 }
1229 }
1230 printf(")\n");
1231 }
1232
1233 int
1234 wdioctl(dev_t dev, u_long cmd, void *addr, int flag, struct lwp *l)
1235 {
1236 struct wd_softc *wd =
1237 device_lookup_private(&wd_cd, WDUNIT(dev));
1238 struct dk_softc *dksc = &wd->sc_dksc;
1239
1240 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1241
1242 if ((wd->sc_flags & WDF_LOADED) == 0)
1243 return EIO;
1244
1245 switch (cmd) {
1246 #ifdef HAS_BAD144_HANDLING
1247 case DIOCSBAD:
1248 if ((flag & FWRITE) == 0)
1249 return EBADF;
1250 dksc->sc_dkdev.dk_cpulabel->bad = *(struct dkbad *)addr;
1251 dksc->sc_dkdev.dk_label->d_flags |= D_BADSECT;
1252 bad144intern(wd);
1253 return 0;
1254 #endif
1255 #ifdef WD_SOFTBADSECT
1256 case DIOCBSLIST :
1257 {
1258 uint32_t count, missing, skip;
1259 struct disk_badsecinfo dbsi;
1260 struct disk_badsectors *dbs;
1261 size_t available;
1262 uint8_t *laddr;
1263
1264 dbsi = *(struct disk_badsecinfo *)addr;
1265 missing = wd->sc_bscount;
1266 count = 0;
1267 available = dbsi.dbsi_bufsize;
1268 skip = dbsi.dbsi_skip;
1269 laddr = (uint8_t *)dbsi.dbsi_buffer;
1270
1271 /*
1272 * We start this loop with the expectation that all of the
1273 * entries will be missed and decrement this counter each
1274 * time we either skip over one (already copied out) or
1275 * we actually copy it back to user space. The structs
1276 * holding the bad sector information are copied directly
1277 * back to user space whilst the summary is returned via
1278 * the struct passed in via the ioctl.
1279 */
1280 mutex_enter(&wd->sc_lock);
1281 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1282 if (skip > 0) {
1283 missing--;
1284 skip--;
1285 continue;
1286 }
1287 if (available < sizeof(*dbs))
1288 break;
1289 available -= sizeof(*dbs);
1290 copyout(dbs, laddr, sizeof(*dbs));
1291 laddr += sizeof(*dbs);
1292 missing--;
1293 count++;
1294 }
1295 mutex_exit(&wd->sc_lock);
1296 dbsi.dbsi_left = missing;
1297 dbsi.dbsi_copied = count;
1298 *(struct disk_badsecinfo *)addr = dbsi;
1299 return 0;
1300 }
1301
1302 case DIOCBSFLUSH :
1303 /* Clean out the bad sector list */
1304 mutex_enter(&wd->sc_lock);
1305 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1306 struct disk_badsectors *dbs =
1307 SLIST_FIRST(&wd->sc_bslist);
1308 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1309 kmem_free(dbs, sizeof(*dbs));
1310 }
1311 mutex_exit(&wd->sc_lock);
1312 wd->sc_bscount = 0;
1313 return 0;
1314 #endif
1315
1316 #ifdef notyet
1317 case DIOCWFORMAT:
1318 if ((flag & FWRITE) == 0)
1319 return EBADF;
1320 {
1321 register struct format_op *fop;
1322 struct iovec aiov;
1323 struct uio auio;
1324 int error1;
1325
1326 fop = (struct format_op *)addr;
1327 aiov.iov_base = fop->df_buf;
1328 aiov.iov_len = fop->df_count;
1329 auio.uio_iov = &aiov;
1330 auio.uio_iovcnt = 1;
1331 auio.uio_resid = fop->df_count;
1332 auio.uio_offset =
1333 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1334 auio.uio_vmspace = l->l_proc->p_vmspace;
1335 error1 = physio(wdformat, NULL, dev, B_WRITE, wdminphys,
1336 &auio);
1337 fop->df_count -= auio.uio_resid;
1338 fop->df_reg[0] = wdc->sc_status;
1339 fop->df_reg[1] = wdc->sc_error;
1340 return error1;
1341 }
1342 #endif
1343 case DIOCGCACHE:
1344 return wd_getcache(wd, (int *)addr);
1345
1346 case DIOCSCACHE:
1347 return wd_setcache(wd, *(int *)addr);
1348
1349 case DIOCCACHESYNC:
1350 return wd_flushcache(wd, AT_WAIT, true);
1351
1352 case ATAIOCCOMMAND:
1353 /*
1354 * Make sure this command is (relatively) safe first
1355 */
1356 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1357 (flag & FWRITE) == 0)
1358 return (EBADF);
1359 {
1360 struct wd_ioctl *wi;
1361 atareq_t *atareq = (atareq_t *) addr;
1362 int error1;
1363
1364 wi = wi_get(wd);
1365 wi->wi_atareq = *atareq;
1366
1367 if (atareq->datalen && atareq->flags &
1368 (ATACMD_READ | ATACMD_WRITE)) {
1369 void *tbuf;
1370 if (atareq->datalen < DEV_BSIZE
1371 && atareq->command == WDCC_IDENTIFY) {
1372 tbuf = kmem_zalloc(DEV_BSIZE, KM_SLEEP);
1373 wi->wi_iov.iov_base = tbuf;
1374 wi->wi_iov.iov_len = DEV_BSIZE;
1375 UIO_SETUP_SYSSPACE(&wi->wi_uio);
1376 } else {
1377 tbuf = NULL;
1378 wi->wi_iov.iov_base = atareq->databuf;
1379 wi->wi_iov.iov_len = atareq->datalen;
1380 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1381 }
1382 wi->wi_uio.uio_iov = &wi->wi_iov;
1383 wi->wi_uio.uio_iovcnt = 1;
1384 wi->wi_uio.uio_resid = atareq->datalen;
1385 wi->wi_uio.uio_offset = 0;
1386 wi->wi_uio.uio_rw =
1387 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1388 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1389 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1390 wdminphys, &wi->wi_uio);
1391 if (tbuf != NULL && error1 == 0) {
1392 error1 = copyout(tbuf, atareq->databuf,
1393 atareq->datalen);
1394 kmem_free(tbuf, DEV_BSIZE);
1395 }
1396 } else {
1397 /* No need to call physio if we don't have any
1398 user data */
1399 wi->wi_bp.b_flags = 0;
1400 wi->wi_bp.b_data = 0;
1401 wi->wi_bp.b_bcount = 0;
1402 wi->wi_bp.b_dev = dev;
1403 wi->wi_bp.b_proc = l->l_proc;
1404 wdioctlstrategy(&wi->wi_bp);
1405 error1 = wi->wi_bp.b_error;
1406 }
1407 *atareq = wi->wi_atareq;
1408 wi_free(wi);
1409 return(error1);
1410 }
1411
1412 default:
1413 return dk_ioctl(dksc, dev, cmd, addr, flag, l);
1414 }
1415
1416 #ifdef DIAGNOSTIC
1417 panic("wdioctl: impossible");
1418 #endif
1419 }
1420
1421 static int
1422 wd_discard(device_t dev, off_t pos, off_t len)
1423 {
1424 struct wd_softc *wd = device_private(dev);
1425 daddr_t bno;
1426 long size, done;
1427 long maxatonce, amount;
1428 int result;
1429
1430 if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7)
1431 || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) {
1432 /* not supported; ignore request */
1433 ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS);
1434 return 0;
1435 }
1436 maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/
1437
1438 ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS);
1439
1440 if ((wd->sc_flags & WDF_LOADED) == 0)
1441 return EIO;
1442
1443 /* round the start up and the end down */
1444 bno = (pos + wd->sc_blksize - 1) / wd->sc_blksize;
1445 size = ((pos + len) / wd->sc_blksize) - bno;
1446
1447 done = 0;
1448 while (done < size) {
1449 amount = size - done;
1450 if (amount > maxatonce) {
1451 amount = maxatonce;
1452 }
1453 result = wd_trim(wd, bno + done, amount);
1454 if (result) {
1455 return result;
1456 }
1457 done += amount;
1458 }
1459 return 0;
1460 }
1461
1462 static int
1463 wddiscard(dev_t dev, off_t pos, off_t len)
1464 {
1465 struct wd_softc *wd;
1466 struct dk_softc *dksc;
1467 int unit;
1468
1469 unit = WDUNIT(dev);
1470 wd = device_lookup_private(&wd_cd, unit);
1471 dksc = &wd->sc_dksc;
1472
1473 return dk_discard(dksc, dev, pos, len);
1474 }
1475
1476 #ifdef B_FORMAT
1477 int
1478 wdformat(struct buf *bp)
1479 {
1480
1481 bp->b_flags |= B_FORMAT;
1482 return wdstrategy(bp);
1483 }
1484 #endif
1485
1486 int
1487 wdsize(dev_t dev)
1488 {
1489 struct wd_softc *wd;
1490 struct dk_softc *dksc;
1491 int unit;
1492
1493 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1494
1495 unit = WDUNIT(dev);
1496 wd = device_lookup_private(&wd_cd, unit);
1497 if (wd == NULL)
1498 return (-1);
1499 dksc = &wd->sc_dksc;
1500
1501 if (!device_is_active(dksc->sc_dev))
1502 return (-1);
1503
1504 return dk_size(dksc, dev);
1505 }
1506
1507 /*
1508 * Dump core after a system crash.
1509 */
1510 static int
1511 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1512 {
1513 struct wd_softc *wd;
1514 struct dk_softc *dksc;
1515 int unit;
1516
1517 /* Check if recursive dump; if so, punt. */
1518 if (wddoingadump)
1519 return EFAULT;
1520 wddoingadump = 1;
1521
1522 unit = WDUNIT(dev);
1523 wd = device_lookup_private(&wd_cd, unit);
1524 if (wd == NULL)
1525 return (ENXIO);
1526 dksc = &wd->sc_dksc;
1527
1528 return dk_dump(dksc, dev, blkno, va, size);
1529 }
1530
1531 static int
1532 wd_dumpblocks(device_t dev, void *va, daddr_t blkno, int nblk)
1533 {
1534 struct wd_softc *wd = device_private(dev);
1535 struct dk_softc *dksc = &wd->sc_dksc;
1536 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
1537 struct ata_xfer *xfer = &wd->dump_xfer;
1538 int err;
1539
1540 /* Recalibrate, if first dump transfer. */
1541 if (wddumprecalibrated == 0) {
1542 wddumprecalibrated = 1;
1543 ata_channel_lock(wd->drvp->chnl_softc);
1544 /* This will directly execute the reset due to AT_POLL */
1545 ata_thread_run(wd->drvp->chnl_softc, AT_POLL,
1546 ATACH_TH_DRIVE_RESET, wd->drvp->drive);
1547
1548 wd->drvp->state = RESET;
1549 ata_channel_unlock(wd->drvp->chnl_softc);
1550 }
1551
1552 memset(xfer, 0, sizeof(*xfer));
1553 xfer->c_flags |= C_PRIVATE_ALLOC | C_SKIP_QUEUE;
1554
1555 xfer->c_bio.blkno = blkno;
1556 xfer->c_bio.flags = ATA_POLL;
1557 if (wd->sc_flags & WDF_LBA48 &&
1558 (xfer->c_bio.blkno + nblk) > wd->sc_capacity28)
1559 xfer->c_bio.flags |= ATA_LBA48;
1560 if (wd->sc_flags & WDF_LBA)
1561 xfer->c_bio.flags |= ATA_LBA;
1562 xfer->c_bio.bcount = nblk * dg->dg_secsize;
1563 xfer->c_bio.databuf = va;
1564 #ifndef WD_DUMP_NOT_TRUSTED
1565 switch (err = wd->atabus->ata_bio(wd->drvp, xfer)) {
1566 case ATACMD_TRY_AGAIN:
1567 panic("wddump: try again");
1568 break;
1569 case ATACMD_QUEUED:
1570 panic("wddump: polled command has been queued");
1571 break;
1572 case ATACMD_COMPLETE:
1573 break;
1574 default:
1575 panic("wddump: unknown atacmd code %d", err);
1576 }
1577 switch(err = xfer->c_bio.error) {
1578 case TIMEOUT:
1579 printf("wddump: device timed out");
1580 err = EIO;
1581 break;
1582 case ERR_DF:
1583 printf("wddump: drive fault");
1584 err = EIO;
1585 break;
1586 case ERR_DMA:
1587 printf("wddump: DMA error");
1588 err = EIO;
1589 break;
1590 case ERROR:
1591 printf("wddump: ");
1592 wdperror(wd, xfer);
1593 err = EIO;
1594 break;
1595 case NOERROR:
1596 err = 0;
1597 break;
1598 default:
1599 panic("wddump: unknown error type %x", err);
1600 }
1601
1602 if (err != 0) {
1603 printf("\n");
1604 return err;
1605 }
1606 #else /* WD_DUMP_NOT_TRUSTED */
1607 /* Let's just talk about this first... */
1608 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1609 unit, va, cylin, head, sector);
1610 delay(500 * 1000); /* half a second */
1611 #endif
1612
1613 wddoingadump = 0;
1614 return 0;
1615 }
1616
1617 #ifdef HAS_BAD144_HANDLING
1618 /*
1619 * Internalize the bad sector table.
1620 */
1621 void
1622 bad144intern(struct wd_softc *wd)
1623 {
1624 struct dk_softc *dksc = &wd->sc_dksc;
1625 struct dkbad *bt = &dksc->sc_dkdev.dk_cpulabel->bad;
1626 struct disklabel *lp = dksc->sc_dkdev.dk_label;
1627 int i = 0;
1628
1629 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1630
1631 for (; i < NBT_BAD; i++) {
1632 if (bt->bt_bad[i].bt_cyl == 0xffff)
1633 break;
1634 wd->drvp->badsect[i] =
1635 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1636 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1637 (bt->bt_bad[i].bt_trksec & 0xff);
1638 }
1639 for (; i < NBT_BAD+1; i++)
1640 wd->drvp->badsect[i] = -1;
1641 }
1642 #endif
1643
1644 static void
1645 wd_set_geometry(struct wd_softc *wd)
1646 {
1647 struct dk_softc *dksc = &wd->sc_dksc;
1648 struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
1649
1650 memset(dg, 0, sizeof(*dg));
1651
1652 dg->dg_secperunit = wd->sc_capacity;
1653 dg->dg_secsize = wd->sc_blksize;
1654 dg->dg_nsectors = wd->sc_params.atap_sectors;
1655 dg->dg_ntracks = wd->sc_params.atap_heads;
1656 if ((wd->sc_flags & WDF_LBA) == 0)
1657 dg->dg_ncylinders = wd->sc_params.atap_cylinders;
1658
1659 disk_set_info(dksc->sc_dev, &dksc->sc_dkdev, wd->sc_typename);
1660 }
1661
1662 int
1663 wd_get_params(struct wd_softc *wd, uint8_t flags, struct ataparams *params)
1664 {
1665 int retry = 0;
1666 struct ata_channel *chp = wd->drvp->chnl_softc;
1667
1668 again:
1669 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1670 case CMD_AGAIN:
1671 return 1;
1672 case CMD_ERR:
1673 if (retry == 0) {
1674 retry++;
1675 ata_channel_lock(chp);
1676 (*wd->atabus->ata_reset_drive)(wd->drvp, flags, NULL);
1677 ata_channel_unlock(chp);
1678 goto again;
1679 }
1680
1681 if (wd->drvp->drive_type != ATA_DRIVET_OLD)
1682 return 1;
1683 /*
1684 * We `know' there's a drive here; just assume it's old.
1685 * This geometry is only used to read the MBR and print a
1686 * (false) attach message.
1687 */
1688 strncpy(params->atap_model, "ST506",
1689 sizeof params->atap_model);
1690 params->atap_config = ATA_CFG_FIXED;
1691 params->atap_cylinders = 1024;
1692 params->atap_heads = 8;
1693 params->atap_sectors = 17;
1694 params->atap_multi = 1;
1695 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1696 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1697 /* FALLTHROUGH */
1698 case CMD_OK:
1699 return 0;
1700 default:
1701 panic("wd_get_params: bad return code from ata_get_params");
1702 /* NOTREACHED */
1703 }
1704 }
1705
1706 int
1707 wd_getcache(struct wd_softc *wd, int *bitsp)
1708 {
1709 struct ataparams params;
1710
1711 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1712 return EIO;
1713 if (params.atap_cmd_set1 == 0x0000 ||
1714 params.atap_cmd_set1 == 0xffff ||
1715 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1716 *bitsp = 0;
1717 return 0;
1718 }
1719 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1720 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1721 *bitsp |= DKCACHE_WRITE;
1722
1723 if (WD_USE_NCQ(wd) || (wd->drvp->drive_flags & ATA_DRIVE_WFUA))
1724 *bitsp |= DKCACHE_FUA;
1725
1726 return 0;
1727 }
1728
1729 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1730
1731 int
1732 wd_setcache(struct wd_softc *wd, int bits)
1733 {
1734 struct dk_softc *dksc = &wd->sc_dksc;
1735 struct ataparams params;
1736 struct ata_xfer *xfer;
1737 int error;
1738
1739 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1740 return EIO;
1741
1742 if (params.atap_cmd_set1 == 0x0000 ||
1743 params.atap_cmd_set1 == 0xffff ||
1744 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1745 return EOPNOTSUPP;
1746
1747 if ((bits & DKCACHE_READ) == 0 ||
1748 (bits & DKCACHE_SAVE) != 0)
1749 return EOPNOTSUPP;
1750
1751 xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
1752
1753 xfer->c_ata_c.r_command = SET_FEATURES;
1754 xfer->c_ata_c.r_st_bmask = 0;
1755 xfer->c_ata_c.r_st_pmask = 0;
1756 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
1757 xfer->c_ata_c.flags = AT_WAIT;
1758 if (bits & DKCACHE_WRITE)
1759 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_EN;
1760 else
1761 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_DS;
1762 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1763 aprint_error_dev(dksc->sc_dev,
1764 "wd_setcache command not complete\n");
1765 error = EIO;
1766 goto out;
1767 }
1768
1769 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1770 char sbuf[sizeof(at_errbits) + 64];
1771 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1772 aprint_error_dev(dksc->sc_dev, "wd_setcache: status=%s\n", sbuf);
1773 error = EIO;
1774 goto out;
1775 }
1776
1777 error = 0;
1778
1779 out:
1780 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1781 return error;
1782 }
1783
1784 static int
1785 wd_standby(struct wd_softc *wd, int flags)
1786 {
1787 struct dk_softc *dksc = &wd->sc_dksc;
1788 struct ata_xfer *xfer;
1789 int error;
1790
1791 aprint_debug_dev(dksc->sc_dev, "standby immediate\n");
1792 xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
1793
1794 xfer->c_ata_c.r_command = WDCC_STANDBY_IMMED;
1795 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
1796 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
1797 xfer->c_ata_c.flags = flags;
1798 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
1799 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1800 aprint_error_dev(dksc->sc_dev,
1801 "standby immediate command didn't complete\n");
1802 error = EIO;
1803 goto out;
1804 }
1805 if (xfer->c_ata_c.flags & AT_ERROR) {
1806 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
1807 /* command not supported */
1808 aprint_debug_dev(dksc->sc_dev,
1809 "standby immediate not supported\n");
1810 error = ENODEV;
1811 goto out;
1812 }
1813 }
1814 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1815 char sbuf[sizeof(at_errbits) + 64];
1816 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1817 aprint_error_dev(dksc->sc_dev, "wd_standby: status=%s\n", sbuf);
1818 error = EIO;
1819 goto out;
1820 }
1821 error = 0;
1822
1823 out:
1824 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1825 return error;
1826 }
1827
1828 int
1829 wd_flushcache(struct wd_softc *wd, int flags, bool start_self)
1830 {
1831 struct dk_softc *dksc = &wd->sc_dksc;
1832 struct ata_xfer *xfer;
1833 int error;
1834
1835 /*
1836 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1837 * only ATA-2 and still support it.
1838 */
1839 if (wd->drvp->ata_vers < 4 &&
1840 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1841 wd->sc_params.atap_cmd_set2 == 0xffff))
1842 return ENODEV;
1843
1844 xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
1845
1846 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1847 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) {
1848 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1849 flags |= AT_LBA48;
1850 } else
1851 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE;
1852 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
1853 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
1854 xfer->c_ata_c.flags = flags | AT_READREG;
1855 xfer->c_ata_c.timeout = 300000; /* 5m timeout */
1856 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1857 aprint_error_dev(dksc->sc_dev,
1858 "flush cache command didn't complete\n");
1859 error = EIO;
1860 goto out_xfer;
1861 }
1862 if (xfer->c_ata_c.flags & AT_ERROR) {
1863 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
1864 /* command not supported */
1865 error = ENODEV;
1866 goto out_xfer;
1867 }
1868 }
1869 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1870 char sbuf[sizeof(at_errbits) + 64];
1871 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1872 aprint_error_dev(dksc->sc_dev, "wd_flushcache: status=%s\n",
1873 sbuf);
1874 error = EIO;
1875 goto out_xfer;
1876 }
1877 wd->sc_flags &= ~WDF_DIRTY;
1878 error = 0;
1879
1880 out_xfer:
1881 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1882 return error;
1883 }
1884
1885 /*
1886 * Execute TRIM command, assumes sleep context.
1887 */
1888 static int
1889 wd_trim(struct wd_softc *wd, daddr_t bno, long size)
1890 {
1891 struct dk_softc *dksc = &wd->sc_dksc;
1892 struct ata_xfer *xfer;
1893 int error;
1894 unsigned char *req;
1895
1896 xfer = ata_get_xfer(wd->drvp->chnl_softc, true);
1897
1898 req = kmem_zalloc(512, KM_SLEEP);
1899 req[0] = bno & 0xff;
1900 req[1] = (bno >> 8) & 0xff;
1901 req[2] = (bno >> 16) & 0xff;
1902 req[3] = (bno >> 24) & 0xff;
1903 req[4] = (bno >> 32) & 0xff;
1904 req[5] = (bno >> 40) & 0xff;
1905 req[6] = size & 0xff;
1906 req[7] = (size >> 8) & 0xff;
1907
1908 /*
1909 * XXX We could possibly use NCQ TRIM, which supports executing
1910 * this command concurrently. It would need some investigation, some
1911 * early or not so early disk firmware caused data loss with NCQ TRIM.
1912 * atastart() et.al would need to be adjusted to allow and support
1913 * running several non-I/O ATA commands in parallel.
1914 */
1915
1916 xfer->c_ata_c.r_command = ATA_DATA_SET_MANAGEMENT;
1917 xfer->c_ata_c.r_count = 1;
1918 xfer->c_ata_c.r_features = ATA_SUPPORT_DSM_TRIM;
1919 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
1920 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
1921 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
1922 xfer->c_ata_c.data = req;
1923 xfer->c_ata_c.bcount = 512;
1924 xfer->c_ata_c.flags |= AT_WRITE | AT_WAIT;
1925 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1926 aprint_error_dev(dksc->sc_dev,
1927 "trim command didn't complete\n");
1928 kmem_free(req, 512);
1929 error = EIO;
1930 goto out;
1931 }
1932 kmem_free(req, 512);
1933 if (xfer->c_ata_c.flags & AT_ERROR) {
1934 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
1935 /* command not supported */
1936 error = ENODEV;
1937 goto out;
1938 }
1939 }
1940 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1941 char sbuf[sizeof(at_errbits) + 64];
1942 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1943 aprint_error_dev(dksc->sc_dev, "wd_trim: status=%s\n",
1944 sbuf);
1945 error = EIO;
1946 goto out;
1947 }
1948 error = 0;
1949
1950 out:
1951 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1952 return error;
1953 }
1954
1955 bool
1956 wd_shutdown(device_t dev, int how)
1957 {
1958 struct wd_softc *wd = device_private(dev);
1959
1960 /* the adapter needs to be enabled */
1961 if (wd->atabus->ata_addref(wd->drvp))
1962 return true; /* no need to complain */
1963
1964 wd_flushcache(wd, AT_POLL, false);
1965 if ((how & RB_POWERDOWN) == RB_POWERDOWN)
1966 wd_standby(wd, AT_POLL);
1967 return true;
1968 }
1969
1970 /*
1971 * Allocate space for a ioctl queue structure. Mostly taken from
1972 * scsipi_ioctl.c
1973 */
1974 struct wd_ioctl *
1975 wi_get(struct wd_softc *wd)
1976 {
1977 struct wd_ioctl *wi;
1978
1979 wi = kmem_zalloc(sizeof(struct wd_ioctl), KM_SLEEP);
1980 wi->wi_softc = wd;
1981 buf_init(&wi->wi_bp);
1982
1983 return (wi);
1984 }
1985
1986 /*
1987 * Free an ioctl structure and remove it from our list
1988 */
1989
1990 void
1991 wi_free(struct wd_ioctl *wi)
1992 {
1993 buf_destroy(&wi->wi_bp);
1994 kmem_free(wi, sizeof(*wi));
1995 }
1996
1997 /*
1998 * Find a wd_ioctl structure based on the struct buf.
1999 */
2000
2001 struct wd_ioctl *
2002 wi_find(struct buf *bp)
2003 {
2004 return container_of(bp, struct wd_ioctl, wi_bp);
2005 }
2006
2007 static uint
2008 wi_sector_size(const struct wd_ioctl * const wi)
2009 {
2010 switch (wi->wi_atareq.command) {
2011 case WDCC_READ:
2012 case WDCC_WRITE:
2013 case WDCC_READMULTI:
2014 case WDCC_WRITEMULTI:
2015 case WDCC_READDMA:
2016 case WDCC_WRITEDMA:
2017 case WDCC_READ_EXT:
2018 case WDCC_WRITE_EXT:
2019 case WDCC_READMULTI_EXT:
2020 case WDCC_WRITEMULTI_EXT:
2021 case WDCC_READDMA_EXT:
2022 case WDCC_WRITEDMA_EXT:
2023 case WDCC_READ_FPDMA_QUEUED:
2024 case WDCC_WRITE_FPDMA_QUEUED:
2025 return wi->wi_softc->sc_blksize;
2026 default:
2027 return 512;
2028 }
2029 }
2030
2031 /*
2032 * Ioctl pseudo strategy routine
2033 *
2034 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
2035 * happens here is:
2036 *
2037 * - wdioctl() queues a wd_ioctl structure.
2038 *
2039 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
2040 * user space I/O is required. If physio() is called, physio() eventually
2041 * calls wdioctlstrategy().
2042 *
2043 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
2044 * to perform the actual command
2045 *
2046 * The reason for the use of the pseudo strategy routine is because
2047 * when doing I/O to/from user space, physio _really_ wants to be in
2048 * the loop. We could put the entire buffer into the ioctl request
2049 * structure, but that won't scale if we want to do things like download
2050 * microcode.
2051 */
2052
2053 void
2054 wdioctlstrategy(struct buf *bp)
2055 {
2056 struct wd_ioctl *wi;
2057 struct ata_xfer *xfer;
2058 int error = 0;
2059
2060 wi = wi_find(bp);
2061 if (wi == NULL) {
2062 printf("wdioctlstrategy: "
2063 "No matching ioctl request found in queue\n");
2064 error = EINVAL;
2065 goto out2;
2066 }
2067
2068 xfer = ata_get_xfer(wi->wi_softc->drvp->chnl_softc, true);
2069
2070 /*
2071 * Abort if physio broke up the transfer
2072 */
2073
2074 if (bp->b_bcount != wi->wi_atareq.datalen) {
2075 printf("physio split wd ioctl request... cannot proceed\n");
2076 error = EIO;
2077 goto out;
2078 }
2079
2080 /*
2081 * Abort if we didn't get a buffer size that was a multiple of
2082 * our sector size (or overflows CHS/LBA28 sector count)
2083 */
2084
2085 if ((bp->b_bcount % wi_sector_size(wi)) != 0 ||
2086 (bp->b_bcount / wi_sector_size(wi)) >=
2087 (1 << NBBY)) {
2088 error = EINVAL;
2089 goto out;
2090 }
2091
2092 /*
2093 * Make sure a timeout was supplied in the ioctl request
2094 */
2095
2096 if (wi->wi_atareq.timeout == 0) {
2097 error = EINVAL;
2098 goto out;
2099 }
2100
2101 if (wi->wi_atareq.flags & ATACMD_READ)
2102 xfer->c_ata_c.flags |= AT_READ;
2103 else if (wi->wi_atareq.flags & ATACMD_WRITE)
2104 xfer->c_ata_c.flags |= AT_WRITE;
2105
2106 if (wi->wi_atareq.flags & ATACMD_READREG)
2107 xfer->c_ata_c.flags |= AT_READREG;
2108
2109 if ((wi->wi_atareq.flags & ATACMD_LBA) != 0)
2110 xfer->c_ata_c.flags |= AT_LBA;
2111
2112 xfer->c_ata_c.flags |= AT_WAIT;
2113
2114 xfer->c_ata_c.timeout = wi->wi_atareq.timeout;
2115 xfer->c_ata_c.r_command = wi->wi_atareq.command;
2116 xfer->c_ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) |
2117 (wi->wi_atareq.cylinder << 8) |
2118 wi->wi_atareq.sec_num;
2119 xfer->c_ata_c.r_count = wi->wi_atareq.sec_count;
2120 xfer->c_ata_c.r_features = wi->wi_atareq.features;
2121 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
2122 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
2123 xfer->c_ata_c.data = wi->wi_bp.b_data;
2124 xfer->c_ata_c.bcount = wi->wi_bp.b_bcount;
2125
2126 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, xfer)
2127 != ATACMD_COMPLETE) {
2128 wi->wi_atareq.retsts = ATACMD_ERROR;
2129 error = EIO;
2130 goto out;
2131 }
2132
2133 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2134 if (xfer->c_ata_c.flags & AT_ERROR) {
2135 wi->wi_atareq.retsts = ATACMD_ERROR;
2136 wi->wi_atareq.error = xfer->c_ata_c.r_error;
2137 } else if (xfer->c_ata_c.flags & AT_DF)
2138 wi->wi_atareq.retsts = ATACMD_DF;
2139 else
2140 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
2141 } else {
2142 wi->wi_atareq.retsts = ATACMD_OK;
2143 if (wi->wi_atareq.flags & ATACMD_READREG) {
2144 wi->wi_atareq.command = xfer->c_ata_c.r_status;
2145 wi->wi_atareq.features = xfer->c_ata_c.r_error;
2146 wi->wi_atareq.sec_count = xfer->c_ata_c.r_count;
2147 wi->wi_atareq.sec_num = xfer->c_ata_c.r_lba & 0xff;
2148 wi->wi_atareq.head = (xfer->c_ata_c.r_device & 0xf0) |
2149 ((xfer->c_ata_c.r_lba >> 24) & 0x0f);
2150 wi->wi_atareq.cylinder =
2151 (xfer->c_ata_c.r_lba >> 8) & 0xffff;
2152 wi->wi_atareq.error = xfer->c_ata_c.r_error;
2153 }
2154 }
2155
2156 out:
2157 ata_free_xfer(wi->wi_softc->drvp->chnl_softc, xfer);
2158 out2:
2159 bp->b_error = error;
2160 if (error)
2161 bp->b_resid = bp->b_bcount;
2162 biodone(bp);
2163 }
2164
2165 static void
2166 wd_sysctl_attach(struct wd_softc *wd)
2167 {
2168 struct dk_softc *dksc = &wd->sc_dksc;
2169 const struct sysctlnode *node;
2170 int error;
2171
2172 /* sysctl set-up */
2173 if (sysctl_createv(&wd->nodelog, 0, NULL, &node,
2174 0, CTLTYPE_NODE, dksc->sc_xname,
2175 SYSCTL_DESCR("wd driver settings"),
2176 NULL, 0, NULL, 0,
2177 CTL_HW, CTL_CREATE, CTL_EOL) != 0) {
2178 aprint_error_dev(dksc->sc_dev,
2179 "could not create %s.%s sysctl node\n",
2180 "hw", dksc->sc_xname);
2181 return;
2182 }
2183
2184 wd->drv_ncq = true;
2185 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
2186 CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq",
2187 SYSCTL_DESCR("use NCQ if supported"),
2188 NULL, 0, &wd->drv_ncq, 0,
2189 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
2190 != 0) {
2191 aprint_error_dev(dksc->sc_dev,
2192 "could not create %s.%s.use_ncq sysctl - error %d\n",
2193 "hw", dksc->sc_xname, error);
2194 return;
2195 }
2196
2197 wd->drv_ncq_prio = false;
2198 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
2199 CTLFLAG_READWRITE, CTLTYPE_BOOL, "use_ncq_prio",
2200 SYSCTL_DESCR("use NCQ PRIORITY if supported"),
2201 NULL, 0, &wd->drv_ncq_prio, 0,
2202 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
2203 != 0) {
2204 aprint_error_dev(dksc->sc_dev,
2205 "could not create %s.%s.use_ncq_prio sysctl - error %d\n",
2206 "hw", dksc->sc_xname, error);
2207 return;
2208 }
2209
2210 #ifdef WD_CHAOS_MONKEY
2211 wd->drv_chaos_freq = 0;
2212 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
2213 CTLFLAG_READWRITE, CTLTYPE_INT, "chaos_freq",
2214 SYSCTL_DESCR("simulated bio read error rate"),
2215 NULL, 0, &wd->drv_chaos_freq, 0,
2216 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
2217 != 0) {
2218 aprint_error_dev(dksc->sc_dev,
2219 "could not create %s.%s.chaos_freq sysctl - error %d\n",
2220 "hw", dksc->sc_xname, error);
2221 return;
2222 }
2223
2224 wd->drv_chaos_cnt = 0;
2225 if ((error = sysctl_createv(&wd->nodelog, 0, NULL, NULL,
2226 CTLFLAG_READONLY, CTLTYPE_INT, "chaos_cnt",
2227 SYSCTL_DESCR("number of processed bio reads"),
2228 NULL, 0, &wd->drv_chaos_cnt, 0,
2229 CTL_HW, node->sysctl_num, CTL_CREATE, CTL_EOL))
2230 != 0) {
2231 aprint_error_dev(dksc->sc_dev,
2232 "could not create %s.%s.chaos_cnt sysctl - error %d\n",
2233 "hw", dksc->sc_xname, error);
2234 return;
2235 }
2236 #endif
2237
2238 }
2239
2240 static void
2241 wd_sysctl_detach(struct wd_softc *wd)
2242 {
2243 sysctl_teardown(&wd->nodelog);
2244 }
2245
2246 #ifdef ATADEBUG
2247 int wddebug(void);
2248
2249 int
2250 wddebug(void)
2251 {
2252 struct wd_softc *wd;
2253 struct dk_softc *dksc;
2254 int unit;
2255
2256 for (unit = 0; unit <= 3; unit++) {
2257 wd = device_lookup_private(&wd_cd, unit);
2258 if (wd == NULL)
2259 continue;
2260 dksc = &wd->sc_dksc;
2261 printf("%s fl %x bufq %p:\n",
2262 dksc->sc_xname, wd->sc_flags, bufq_peek(dksc->sc_bufq));
2263
2264 atachannel_debug(wd->drvp->chnl_softc);
2265 }
2266 return 0;
2267 }
2268 #endif /* ATADEBUG */
2269