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