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