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