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