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