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