wd.c revision 1.428.2.29 1 /* $NetBSD: wd.c,v 1.428.2.29 2017/07/23 13:50:43 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.29 2017/07/23 13:50:43 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, false,
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 for the first attempt.
738 * Since device cancels all outstanding requests on error, downgrade
739 * to non-NCQ on retry, so that the retried transfer would not cause
740 * cascade failure for the other transfers if it fails again.
741 * If FUA was requested, we can't downgrade, as that would violate
742 * the semantics - FUA would not be honored. In that case, continue
743 * retrying with NCQ.
744 */
745 if (wd->drvp->drive_flags & ATA_DRIVE_NCQ &&
746 (xfer->c_retries == 0 || (bp->b_flags & B_MEDIA_FUA))) {
747 xfer->c_bio.flags |= ATA_LBA48;
748 xfer->c_flags |= C_NCQ;
749
750 if ((wd->drvp->drive_flags & ATA_DRIVE_NCQ_PRIO) &&
751 BIO_GETPRIO(bp) == BPRIO_TIMECRITICAL)
752 xfer->c_bio.flags |= ATA_PRIO_HIGH;
753 }
754
755 if (wd->sc_flags & WDF_LBA)
756 xfer->c_bio.flags |= ATA_LBA;
757 if (bp->b_flags & B_READ)
758 xfer->c_bio.flags |= ATA_READ;
759 if (bp->b_flags & B_MEDIA_FUA) {
760 /* If not using NCQ, the command WRITE DMA FUA EXT is LBA48 */
761 KASSERT((wd->sc_flags & WDF_LBA48) != 0);
762 if ((xfer->c_flags & C_NCQ) == 0)
763 xfer->c_bio.flags |= ATA_LBA48;
764
765 xfer->c_bio.flags |= ATA_FUA;
766 }
767
768 /* Instrumentation. */
769 if (xfer->c_retries == 0)
770 disk_busy(&wd->sc_dk);
771 switch (wd->atabus->ata_bio(wd->drvp, xfer)) {
772 case ATACMD_TRY_AGAIN:
773 panic("wdstart1: try again");
774 break;
775 case ATACMD_QUEUED:
776 case ATACMD_COMPLETE:
777 break;
778 default:
779 panic("wdstart1: bad return code from ata_bio()");
780 }
781 }
782
783 void
784 wddone(device_t self, struct ata_xfer *xfer)
785 {
786 struct wd_softc *wd = device_private(self);
787 const char *errmsg;
788 int do_perror = 0;
789 struct buf *bp;
790
791 ATADEBUG_PRINT(("wddone %s\n", device_xname(wd->sc_dev)),
792 DEBUG_XFERS);
793
794 if (__predict_false(wddoingadump)) {
795 /* just drop it to the floor */
796 ata_free_xfer(wd->drvp->chnl_softc, xfer);
797 return;
798 }
799
800 bp = xfer->c_bio.bp;
801 KASSERT(bp != NULL);
802
803 bp->b_resid = xfer->c_bio.bcount;
804 switch (xfer->c_bio.error) {
805 case ERR_DMA:
806 errmsg = "DMA error";
807 goto retry;
808 case ERR_DF:
809 errmsg = "device fault";
810 goto retry;
811 case TIMEOUT:
812 errmsg = "device timeout";
813 goto retry;
814 case REQUEUE:
815 errmsg = "requeue";
816 goto retry2;
817 case ERR_RESET:
818 errmsg = "channel reset";
819 goto retry2;
820 case ERROR:
821 /* Don't care about media change bits */
822 if (xfer->c_bio.r_error != 0 &&
823 (xfer->c_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
824 goto noerror;
825 errmsg = "error";
826 do_perror = 1;
827 retry: /* Just reset and retry. Can we do more ? */
828 (*wd->atabus->ata_reset_drive)(wd->drvp, AT_POLL, NULL);
829 retry2:
830 mutex_enter(&wd->sc_lock);
831
832 diskerr(bp, "wd", errmsg, LOG_PRINTF,
833 xfer->c_bio.blkdone, wd->sc_dk.dk_label);
834 if (xfer->c_retries < WDIORETRIES)
835 printf(", slot %d, retry %d", xfer->c_slot,
836 xfer->c_retries + 1);
837 printf("\n");
838 if (do_perror)
839 wdperror(wd, xfer);
840
841 if (xfer->c_retries < WDIORETRIES) {
842 int timo;
843
844 if (xfer->c_bio.error == REQUEUE) {
845 /* rerun ASAP, and do not count as retry */
846 timo = 1;
847 } else {
848 xfer->c_retries++;
849 timo = RECOVERYTIME;
850 }
851
852 callout_reset(&xfer->c_retry_callout, timo,
853 wdbiorestart, xfer);
854
855 mutex_exit(&wd->sc_lock);
856 return;
857 }
858
859 mutex_exit(&wd->sc_lock);
860
861 #ifdef WD_SOFTBADSECT
862 /*
863 * Not all errors indicate a failed block but those that do,
864 * put the block on the bad-block list for the device. Only
865 * do this for reads because the drive should do it for writes,
866 * itself, according to Manuel.
867 */
868 if ((bp->b_flags & B_READ) &&
869 ((wd->drvp->ata_vers >= 4 && xfer->c_bio.r_error & 64) ||
870 (wd->drvp->ata_vers < 4 && xfer->c_bio.r_error & 192))) {
871 struct disk_badsectors *dbs;
872
873 dbs = malloc(sizeof *dbs, M_TEMP, M_NOWAIT);
874 if (dbs == NULL) {
875 aprint_error_dev(wd->sc_dev,
876 "failed to add bad block to list\n");
877 goto out;
878 }
879
880 dbs->dbs_min = bp->b_rawblkno;
881 dbs->dbs_max = dbs->dbs_min +
882 (bp->b_bcount /wd->sc_blksize) - 1;
883 microtime(&dbs->dbs_failedat);
884
885 mutex_enter(&wd->sc_lock);
886 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
887 wd->sc_bscount++;
888 mutex_exit(&wd->sc_lock);
889 }
890 out:
891 #endif
892 bp->b_error = EIO;
893 break;
894 case NOERROR:
895 noerror: if ((xfer->c_bio.flags & ATA_CORR) || xfer->c_retries > 0)
896 aprint_error_dev(wd->sc_dev,
897 "soft error (corrected)\n");
898 #ifdef WD_CHAOS_MONKEY
899 KASSERT((xfer->c_flags & C_CHAOS) == 0);
900 #endif
901 break;
902 case ERR_NODEV:
903 bp->b_error = EIO;
904 break;
905 }
906 if (__predict_false(bp->b_error != 0) && bp->b_resid == 0) {
907 /*
908 * the disk or controller sometimes report a complete
909 * xfer, when there has been an error. This is wrong,
910 * assume nothing got transfered in this case
911 */
912 bp->b_resid = bp->b_bcount;
913 }
914 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
915 (bp->b_flags & B_READ));
916 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
917 ata_free_xfer(wd->drvp->chnl_softc, xfer);
918 biodone(bp);
919 ata_channel_start(wd->drvp->chnl_softc, wd->drvp->drive);
920 }
921
922 static void
923 wdbiorestart(void *v)
924 {
925 struct ata_xfer *xfer = v;
926 struct buf *bp = xfer->c_bio.bp;
927 struct wd_softc *wd = device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
928
929 ATADEBUG_PRINT(("wdrestart %s\n", device_xname(wd->sc_dev)),
930 DEBUG_XFERS);
931
932 mutex_enter(&wd->sc_lock);
933 wdstart1(wd, bp, xfer);
934 mutex_exit(&wd->sc_lock);
935 }
936
937 static void
938 wdminphys(struct buf *bp)
939 {
940 const struct wd_softc * const wd =
941 device_lookup_private(&wd_cd, WDUNIT(bp->b_dev));
942 uint32_t maxsectors;
943
944 /*
945 * The limit is actually 65536 for LBA48 and 256 for non-LBA48,
946 * but that requires to set the count for the ATA command
947 * to 0, which is somewhat error prone, so better stay safe.
948 */
949 if (wd->sc_flags & WDF_LBA48)
950 maxsectors = 65535;
951 else
952 maxsectors = 128;
953
954 if (bp->b_bcount > (wd->sc_blksize * maxsectors))
955 bp->b_bcount = (wd->sc_blksize * maxsectors);
956
957 minphys(bp);
958 }
959
960 int
961 wdread(dev_t dev, struct uio *uio, int flags)
962 {
963
964 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
965 return (physio(wdstrategy, NULL, dev, B_READ, wdminphys, uio));
966 }
967
968 int
969 wdwrite(dev_t dev, struct uio *uio, int flags)
970 {
971
972 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
973 return (physio(wdstrategy, NULL, dev, B_WRITE, wdminphys, uio));
974 }
975
976 int
977 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
978 {
979 struct wd_softc *wd;
980 int part, error;
981
982 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
983 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
984 if (wd == NULL)
985 return (ENXIO);
986
987 if (! device_is_active(wd->sc_dev))
988 return (ENODEV);
989
990 if (wd->sc_capacity == 0)
991 return (ENODEV);
992
993 part = WDPART(dev);
994
995 mutex_enter(&wd->sc_dk.dk_openlock);
996
997 /*
998 * If there are wedges, and this is not RAW_PART, then we
999 * need to fail.
1000 */
1001 if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
1002 error = EBUSY;
1003 goto bad1;
1004 }
1005
1006 /*
1007 * If this is the first open of this device, add a reference
1008 * to the adapter.
1009 */
1010 if (wd->sc_dk.dk_openmask == 0 &&
1011 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
1012 goto bad1;
1013
1014 if (wd->sc_dk.dk_openmask != 0) {
1015 /*
1016 * If any partition is open, but the disk has been invalidated,
1017 * disallow further opens.
1018 */
1019 if ((wd->sc_flags & WDF_LOADED) == 0) {
1020 error = EIO;
1021 goto bad2;
1022 }
1023 } else {
1024 if ((wd->sc_flags & WDF_LOADED) == 0) {
1025
1026 /* Load the physical device parameters. */
1027 if (wd_get_params(wd, AT_WAIT, &wd->sc_params) != 0) {
1028 aprint_error_dev(wd->sc_dev,
1029 "IDENTIFY failed\n");
1030 error = EIO;
1031 goto bad2;
1032 }
1033 wd->sc_flags |= WDF_LOADED;
1034 /* Load the partition info if not already loaded. */
1035 wdgetdisklabel(wd);
1036 }
1037 }
1038
1039 /* Check that the partition exists. */
1040 if (part != RAW_PART &&
1041 (part >= wd->sc_dk.dk_label->d_npartitions ||
1042 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
1043 error = ENXIO;
1044 goto bad2;
1045 }
1046
1047 /* Insure only one open at a time. */
1048 switch (fmt) {
1049 case S_IFCHR:
1050 wd->sc_dk.dk_copenmask |= (1 << part);
1051 break;
1052 case S_IFBLK:
1053 wd->sc_dk.dk_bopenmask |= (1 << part);
1054 break;
1055 }
1056 wd->sc_dk.dk_openmask =
1057 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1058
1059 mutex_exit(&wd->sc_dk.dk_openlock);
1060 return 0;
1061
1062 bad2:
1063 if (wd->sc_dk.dk_openmask == 0)
1064 wd->atabus->ata_delref(wd->drvp);
1065 bad1:
1066 mutex_exit(&wd->sc_dk.dk_openlock);
1067 return error;
1068 }
1069
1070 /*
1071 * Caller must hold wd->sc_dk.dk_openlock.
1072 */
1073 static int
1074 wdlastclose(device_t self)
1075 {
1076 struct wd_softc *wd = device_private(self);
1077
1078 wd_flushcache(wd, AT_WAIT, false);
1079
1080 if (! (wd->sc_flags & WDF_KLABEL))
1081 wd->sc_flags &= ~WDF_LOADED;
1082
1083 wd->atabus->ata_delref(wd->drvp);
1084
1085 return 0;
1086 }
1087
1088 int
1089 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
1090 {
1091 struct wd_softc *wd =
1092 device_lookup_private(&wd_cd, WDUNIT(dev));
1093 int part = WDPART(dev);
1094
1095 ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
1096
1097 mutex_enter(&wd->sc_dk.dk_openlock);
1098
1099 switch (fmt) {
1100 case S_IFCHR:
1101 wd->sc_dk.dk_copenmask &= ~(1 << part);
1102 break;
1103 case S_IFBLK:
1104 wd->sc_dk.dk_bopenmask &= ~(1 << part);
1105 break;
1106 }
1107 wd->sc_dk.dk_openmask =
1108 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
1109
1110 if (wd->sc_dk.dk_openmask == 0)
1111 wdlastclose(wd->sc_dev);
1112
1113 mutex_exit(&wd->sc_dk.dk_openlock);
1114 return 0;
1115 }
1116
1117 void
1118 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
1119 {
1120
1121 ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
1122 memset(lp, 0, sizeof(struct disklabel));
1123
1124 lp->d_secsize = wd->sc_blksize;
1125 lp->d_ntracks = wd->sc_params.atap_heads;
1126 lp->d_nsectors = wd->sc_params.atap_sectors;
1127 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
1128 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
1129 wd->sc_params.atap_cylinders;
1130 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1131
1132 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1133 lp->d_type = DKTYPE_ST506;
1134 else
1135 lp->d_type = DKTYPE_ESDI;
1136
1137 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1138 strncpy(lp->d_packname, "fictitious", 16);
1139 if (wd->sc_capacity > UINT32_MAX)
1140 lp->d_secperunit = UINT32_MAX;
1141 else
1142 lp->d_secperunit = wd->sc_capacity;
1143 lp->d_rpm = 3600;
1144 lp->d_interleave = 1;
1145 lp->d_flags = 0;
1146
1147 lp->d_partitions[RAW_PART].p_offset = 0;
1148 lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
1149 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1150 lp->d_npartitions = RAW_PART + 1;
1151
1152 lp->d_magic = DISKMAGIC;
1153 lp->d_magic2 = DISKMAGIC;
1154 lp->d_checksum = dkcksum(lp);
1155 }
1156
1157 /*
1158 * Fabricate a default disk label, and try to read the correct one.
1159 */
1160 void
1161 wdgetdisklabel(struct wd_softc *wd)
1162 {
1163 struct disklabel *lp = wd->sc_dk.dk_label;
1164 const char *errstring;
1165
1166 ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1167
1168 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1169
1170 wdgetdefaultlabel(wd, lp);
1171
1172 wd->drvp->badsect[0] = -1;
1173
1174 if (wd->drvp->state > RESET) {
1175 mutex_enter(&wd->sc_lock);
1176 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1177 mutex_exit(&wd->sc_lock);
1178 }
1179 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1180 RAW_PART), wdstrategy, lp,
1181 wd->sc_dk.dk_cpulabel);
1182 if (errstring) {
1183 /*
1184 * This probably happened because the drive's default
1185 * geometry doesn't match the DOS geometry. We
1186 * assume the DOS geometry is now in the label and try
1187 * again. XXX This is a kluge.
1188 */
1189 if (wd->drvp->state > RESET) {
1190 mutex_enter(&wd->sc_lock);
1191 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1192 mutex_exit(&wd->sc_lock);
1193 }
1194 errstring = readdisklabel(MAKEWDDEV(0, device_unit(wd->sc_dev),
1195 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1196 }
1197 if (errstring) {
1198 aprint_error_dev(wd->sc_dev, "%s\n", errstring);
1199 return;
1200 }
1201
1202 if (wd->drvp->state > RESET) {
1203 mutex_enter(&wd->sc_lock);
1204 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1205 mutex_exit(&wd->sc_lock);
1206 }
1207 #ifdef HAS_BAD144_HANDLING
1208 if ((lp->d_flags & D_BADSECT) != 0)
1209 bad144intern(wd);
1210 #endif
1211 }
1212
1213 void
1214 wdperror(const struct wd_softc *wd, struct ata_xfer *xfer)
1215 {
1216 static const char *const errstr0_3[] = {"address mark not found",
1217 "track 0 not found", "aborted command", "media change requested",
1218 "id not found", "media changed", "uncorrectable data error",
1219 "bad block detected"};
1220 static const char *const errstr4_5[] = {
1221 "obsolete (address mark not found)",
1222 "no media/write protected", "aborted command",
1223 "media change requested", "id not found", "media changed",
1224 "uncorrectable data error", "interface CRC error"};
1225 const char *const *errstr;
1226 int i;
1227 const char *sep = "";
1228
1229 const char *devname = device_xname(wd->sc_dev);
1230 struct ata_drive_datas *drvp = wd->drvp;
1231 int errno = xfer->c_bio.r_error;
1232
1233 if (drvp->ata_vers >= 4)
1234 errstr = errstr4_5;
1235 else
1236 errstr = errstr0_3;
1237
1238 printf("%s: (", devname);
1239
1240 if (errno == 0)
1241 printf("error not notified");
1242
1243 for (i = 0; i < 8; i++) {
1244 if (errno & (1 << i)) {
1245 printf("%s%s", sep, errstr[i]);
1246 sep = ", ";
1247 }
1248 }
1249 printf(")\n");
1250 }
1251
1252 int
1253 wdioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
1254 {
1255 struct wd_softc *wd =
1256 device_lookup_private(&wd_cd, WDUNIT(dev));
1257 int error;
1258 #ifdef __HAVE_OLD_DISKLABEL
1259 struct disklabel *newlabel = NULL;
1260 #endif
1261
1262 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1263
1264 if ((wd->sc_flags & WDF_LOADED) == 0)
1265 return EIO;
1266
1267 error = disk_ioctl(&wd->sc_dk, dev, xfer, addr, flag, l);
1268 if (error != EPASSTHROUGH)
1269 return error;
1270
1271 error = 0;
1272 switch (xfer) {
1273 #ifdef HAS_BAD144_HANDLING
1274 case DIOCSBAD:
1275 if ((flag & FWRITE) == 0)
1276 return EBADF;
1277 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1278 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1279 bad144intern(wd);
1280 return 0;
1281 #endif
1282 #ifdef WD_SOFTBADSECT
1283 case DIOCBSLIST :
1284 {
1285 uint32_t count, missing, skip;
1286 struct disk_badsecinfo dbsi;
1287 struct disk_badsectors *dbs;
1288 size_t available;
1289 uint8_t *laddr;
1290
1291 dbsi = *(struct disk_badsecinfo *)addr;
1292 missing = wd->sc_bscount;
1293 count = 0;
1294 available = dbsi.dbsi_bufsize;
1295 skip = dbsi.dbsi_skip;
1296 laddr = (uint8_t *)dbsi.dbsi_buffer;
1297
1298 /*
1299 * We start this loop with the expectation that all of the
1300 * entries will be missed and decrement this counter each
1301 * time we either skip over one (already copied out) or
1302 * we actually copy it back to user space. The structs
1303 * holding the bad sector information are copied directly
1304 * back to user space whilst the summary is returned via
1305 * the struct passed in via the ioctl.
1306 */
1307 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1308 if (skip > 0) {
1309 missing--;
1310 skip--;
1311 continue;
1312 }
1313 if (available < sizeof(*dbs))
1314 break;
1315 available -= sizeof(*dbs);
1316 copyout(dbs, laddr, sizeof(*dbs));
1317 laddr += sizeof(*dbs);
1318 missing--;
1319 count++;
1320 }
1321 dbsi.dbsi_left = missing;
1322 dbsi.dbsi_copied = count;
1323 *(struct disk_badsecinfo *)addr = dbsi;
1324 return 0;
1325 }
1326
1327 case DIOCBSFLUSH :
1328 /* Clean out the bad sector list */
1329 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1330 void *head = SLIST_FIRST(&wd->sc_bslist);
1331 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1332 free(head, M_TEMP);
1333 }
1334 wd->sc_bscount = 0;
1335 return 0;
1336 #endif
1337
1338 case DIOCWDINFO:
1339 case DIOCSDINFO:
1340 #ifdef __HAVE_OLD_DISKLABEL
1341 case ODIOCWDINFO:
1342 case ODIOCSDINFO:
1343 #endif
1344 {
1345 struct disklabel *lp;
1346
1347 if ((flag & FWRITE) == 0)
1348 return EBADF;
1349
1350 #ifdef __HAVE_OLD_DISKLABEL
1351 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1352 newlabel = malloc(sizeof *newlabel, M_TEMP,
1353 M_WAITOK | M_ZERO);
1354 if (newlabel == NULL)
1355 return EIO;
1356 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1357 lp = newlabel;
1358 } else
1359 #endif
1360 lp = (struct disklabel *)addr;
1361
1362 mutex_enter(&wd->sc_dk.dk_openlock);
1363 wd->sc_flags |= WDF_LABELLING;
1364
1365 error = setdisklabel(wd->sc_dk.dk_label,
1366 lp, /*wd->sc_dk.dk_openmask : */0,
1367 wd->sc_dk.dk_cpulabel);
1368 if (error == 0) {
1369 if (wd->drvp->state > RESET) {
1370 mutex_enter(&wd->sc_lock);
1371 wd->drvp->drive_flags |= ATA_DRIVE_RESET;
1372 mutex_exit(&wd->sc_lock);
1373 }
1374 if (xfer == DIOCWDINFO
1375 #ifdef __HAVE_OLD_DISKLABEL
1376 || xfer == ODIOCWDINFO
1377 #endif
1378 )
1379 error = writedisklabel(WDLABELDEV(dev),
1380 wdstrategy, wd->sc_dk.dk_label,
1381 wd->sc_dk.dk_cpulabel);
1382 }
1383
1384 wd->sc_flags &= ~WDF_LABELLING;
1385 mutex_exit(&wd->sc_dk.dk_openlock);
1386 #ifdef __HAVE_OLD_DISKLABEL
1387 if (newlabel != NULL)
1388 free(newlabel, M_TEMP);
1389 #endif
1390 return error;
1391 }
1392
1393 case DIOCKLABEL:
1394 if (*(int *)addr)
1395 wd->sc_flags |= WDF_KLABEL;
1396 else
1397 wd->sc_flags &= ~WDF_KLABEL;
1398 return 0;
1399
1400 case DIOCWLABEL:
1401 if ((flag & FWRITE) == 0)
1402 return EBADF;
1403 if (*(int *)addr)
1404 wd->sc_flags |= WDF_WLABEL;
1405 else
1406 wd->sc_flags &= ~WDF_WLABEL;
1407 return 0;
1408
1409 case DIOCGDEFLABEL:
1410 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1411 return 0;
1412 #ifdef __HAVE_OLD_DISKLABEL
1413 case ODIOCGDEFLABEL:
1414 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1415 if (newlabel == NULL)
1416 return EIO;
1417 wdgetdefaultlabel(wd, newlabel);
1418 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1419 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1420 else
1421 error = ENOTTY;
1422 free(newlabel, M_TEMP);
1423 return error;
1424 #endif
1425
1426 #ifdef notyet
1427 case DIOCWFORMAT:
1428 if ((flag & FWRITE) == 0)
1429 return EBADF;
1430 {
1431 register struct format_op *fop;
1432 struct iovec aiov;
1433 struct uio auio;
1434
1435 fop = (struct format_op *)addr;
1436 aiov.iov_base = fop->df_buf;
1437 aiov.iov_len = fop->df_count;
1438 auio.uio_iov = &aiov;
1439 auio.uio_iovcnt = 1;
1440 auio.uio_resid = fop->df_count;
1441 auio.uio_offset =
1442 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1443 auio.uio_vmspace = l->l_proc->p_vmspace;
1444 error = physio(wdformat, NULL, dev, B_WRITE, wdminphys,
1445 &auio);
1446 fop->df_count -= auio.uio_resid;
1447 fop->df_reg[0] = wdc->sc_status;
1448 fop->df_reg[1] = wdc->sc_error;
1449 return error;
1450 }
1451 #endif
1452 case DIOCGCACHE:
1453 return wd_getcache(wd, (int *)addr);
1454
1455 case DIOCSCACHE:
1456 return wd_setcache(wd, *(int *)addr);
1457
1458 case DIOCCACHESYNC:
1459 return wd_flushcache(wd, AT_WAIT, true);
1460
1461 case ATAIOCCOMMAND:
1462 /*
1463 * Make sure this command is (relatively) safe first
1464 */
1465 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1466 (flag & FWRITE) == 0)
1467 return (EBADF);
1468 {
1469 struct wd_ioctl *wi;
1470 atareq_t *atareq = (atareq_t *) addr;
1471 int error1;
1472
1473 wi = wi_get(wd);
1474 wi->wi_atareq = *atareq;
1475
1476 if (atareq->datalen && atareq->flags &
1477 (ATACMD_READ | ATACMD_WRITE)) {
1478 void *tbuf;
1479 if (atareq->datalen < DEV_BSIZE
1480 && atareq->command == WDCC_IDENTIFY) {
1481 tbuf = malloc(DEV_BSIZE, M_TEMP, M_WAITOK);
1482 wi->wi_iov.iov_base = tbuf;
1483 wi->wi_iov.iov_len = DEV_BSIZE;
1484 UIO_SETUP_SYSSPACE(&wi->wi_uio);
1485 } else {
1486 tbuf = NULL;
1487 wi->wi_iov.iov_base = atareq->databuf;
1488 wi->wi_iov.iov_len = atareq->datalen;
1489 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1490 }
1491 wi->wi_uio.uio_iov = &wi->wi_iov;
1492 wi->wi_uio.uio_iovcnt = 1;
1493 wi->wi_uio.uio_resid = atareq->datalen;
1494 wi->wi_uio.uio_offset = 0;
1495 wi->wi_uio.uio_rw =
1496 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1497 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1498 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1499 wdminphys, &wi->wi_uio);
1500 if (tbuf != NULL && error1 == 0) {
1501 error1 = copyout(tbuf, atareq->databuf,
1502 atareq->datalen);
1503 free(tbuf, M_TEMP);
1504 }
1505 } else {
1506 /* No need to call physio if we don't have any
1507 user data */
1508 wi->wi_bp.b_flags = 0;
1509 wi->wi_bp.b_data = 0;
1510 wi->wi_bp.b_bcount = 0;
1511 wi->wi_bp.b_dev = dev;
1512 wi->wi_bp.b_proc = l->l_proc;
1513 wdioctlstrategy(&wi->wi_bp);
1514 error1 = wi->wi_bp.b_error;
1515 }
1516 *atareq = wi->wi_atareq;
1517 wi_free(wi);
1518 return(error1);
1519 }
1520
1521 case DIOCGSTRATEGY:
1522 {
1523 struct disk_strategy *dks = (void *)addr;
1524
1525 mutex_enter(&wd->sc_lock);
1526 strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q),
1527 sizeof(dks->dks_name));
1528 mutex_exit(&wd->sc_lock);
1529 dks->dks_paramlen = 0;
1530
1531 return 0;
1532 }
1533
1534 case DIOCSSTRATEGY:
1535 {
1536 struct disk_strategy *dks = (void *)addr;
1537 struct bufq_state *new;
1538 struct bufq_state *old;
1539
1540 if ((flag & FWRITE) == 0) {
1541 return EBADF;
1542 }
1543 if (dks->dks_param != NULL) {
1544 return EINVAL;
1545 }
1546 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1547 error = bufq_alloc(&new, dks->dks_name,
1548 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1549 if (error) {
1550 return error;
1551 }
1552 mutex_enter(&wd->sc_lock);
1553 old = wd->sc_q;
1554 bufq_move(new, old);
1555 wd->sc_q = new;
1556 mutex_exit(&wd->sc_lock);
1557 bufq_free(old);
1558
1559 return 0;
1560 }
1561
1562 default:
1563 return ENOTTY;
1564 }
1565
1566 #ifdef DIAGNOSTIC
1567 panic("wdioctl: impossible");
1568 #endif
1569 }
1570
1571 static int
1572 wddiscard(dev_t dev, off_t pos, off_t len)
1573 {
1574 struct wd_softc *wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1575 daddr_t bno;
1576 long size, done;
1577 long maxatonce, amount;
1578 int result;
1579
1580 if (!(wd->sc_params.atap_ata_major & WDC_VER_ATA7)
1581 || !(wd->sc_params.support_dsm & ATA_SUPPORT_DSM_TRIM)) {
1582 /* not supported; ignore request */
1583 ATADEBUG_PRINT(("wddiscard (unsupported)\n"), DEBUG_FUNCS);
1584 return 0;
1585 }
1586 maxatonce = 0xffff; /*wd->sc_params.max_dsm_blocks*/
1587
1588 ATADEBUG_PRINT(("wddiscard\n"), DEBUG_FUNCS);
1589
1590 if ((wd->sc_flags & WDF_LOADED) == 0)
1591 return EIO;
1592
1593 /* round the start up and the end down */
1594 bno = (pos + wd->sc_blksize - 1) / wd->sc_blksize;
1595 size = ((pos + len) / wd->sc_blksize) - bno;
1596
1597 done = 0;
1598 while (done < size) {
1599 amount = size - done;
1600 if (amount > maxatonce) {
1601 amount = maxatonce;
1602 }
1603 result = wd_trim(wd, WDPART(dev), bno + done, amount);
1604 if (result) {
1605 return result;
1606 }
1607 done += amount;
1608 }
1609 return 0;
1610 }
1611
1612 #ifdef B_FORMAT
1613 int
1614 wdformat(struct buf *bp)
1615 {
1616
1617 bp->b_flags |= B_FORMAT;
1618 return wdstrategy(bp);
1619 }
1620 #endif
1621
1622 int
1623 wdsize(dev_t dev)
1624 {
1625 struct wd_softc *wd;
1626 int part, omask;
1627 int size;
1628
1629 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1630
1631 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1632 if (wd == NULL)
1633 return (-1);
1634
1635 part = WDPART(dev);
1636 omask = wd->sc_dk.dk_openmask & (1 << part);
1637
1638 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1639 return (-1);
1640 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1641 size = -1;
1642 else
1643 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1644 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1645 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1646 return (-1);
1647 return (size);
1648 }
1649
1650 /*
1651 * Dump core after a system crash.
1652 */
1653 int
1654 wddump(dev_t dev, daddr_t blkno, void *va, size_t size)
1655 {
1656 struct wd_softc *wd; /* disk unit to do the I/O */
1657 struct disklabel *lp; /* disk's disklabel */
1658 int part, err;
1659 int nblks; /* total number of sectors left to write */
1660 struct ata_xfer *xfer;
1661
1662 /* Check if recursive dump; if so, punt. */
1663 if (wddoingadump)
1664 return EFAULT;
1665 wddoingadump = 1;
1666
1667 wd = device_lookup_private(&wd_cd, WDUNIT(dev));
1668 if (wd == NULL)
1669 return (ENXIO);
1670
1671 part = WDPART(dev);
1672
1673 /* Convert to disk sectors. Request must be a multiple of size. */
1674 lp = wd->sc_dk.dk_label;
1675 if ((size % lp->d_secsize) != 0)
1676 return EFAULT;
1677 nblks = size / lp->d_secsize;
1678 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1679
1680 /* Check transfer bounds against partition size. */
1681 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1682 return EINVAL;
1683
1684 /* Offset block number to start of partition. */
1685 blkno += lp->d_partitions[part].p_offset;
1686
1687 /* Recalibrate, if first dump transfer. */
1688 if (wddumprecalibrated == 0) {
1689 wddumprecalibrated = 1;
1690 (*wd->atabus->ata_reset_drive)(wd->drvp,
1691 AT_POLL | AT_RST_EMERG, NULL);
1692 wd->drvp->state = RESET;
1693 }
1694
1695 xfer = ata_get_xfer_ext(wd->drvp->chnl_softc, false, 0);
1696 if (xfer == NULL) {
1697 printf("%s: no xfer\n", __func__);
1698 return EAGAIN;
1699 }
1700
1701 xfer->c_bio.blkno = blkno;
1702 xfer->c_bio.flags = ATA_POLL;
1703 if (wd->sc_flags & WDF_LBA48 &&
1704 (xfer->c_bio.blkno + nblks) > wd->sc_capacity28)
1705 xfer->c_bio.flags |= ATA_LBA48;
1706 if (wd->sc_flags & WDF_LBA)
1707 xfer->c_bio.flags |= ATA_LBA;
1708 xfer->c_bio.bcount = nblks * lp->d_secsize;
1709 xfer->c_bio.databuf = va;
1710 #ifndef WD_DUMP_NOT_TRUSTED
1711 switch (err = wd->atabus->ata_bio(wd->drvp, xfer)) {
1712 case ATACMD_TRY_AGAIN:
1713 panic("wddump: try again");
1714 break;
1715 case ATACMD_QUEUED:
1716 panic("wddump: polled command has been queued");
1717 break;
1718 case ATACMD_COMPLETE:
1719 break;
1720 default:
1721 panic("wddump: unknown atacmd code %d", err);
1722 }
1723 switch(err = xfer->c_bio.error) {
1724 case TIMEOUT:
1725 printf("wddump: device timed out");
1726 err = EIO;
1727 break;
1728 case ERR_DF:
1729 printf("wddump: drive fault");
1730 err = EIO;
1731 break;
1732 case ERR_DMA:
1733 printf("wddump: DMA error");
1734 err = EIO;
1735 break;
1736 case ERROR:
1737 printf("wddump: ");
1738 wdperror(wd, xfer);
1739 err = EIO;
1740 break;
1741 case NOERROR:
1742 err = 0;
1743 break;
1744 default:
1745 panic("wddump: unknown error type %d", err);
1746 }
1747
1748 if (err != 0) {
1749 printf("\n");
1750 return err;
1751 }
1752 #else /* WD_DUMP_NOT_TRUSTED */
1753 /* Let's just talk about this first... */
1754 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1755 unit, va, cylin, head, sector);
1756 delay(500 * 1000); /* half a second */
1757 #endif
1758
1759 wddoingadump = 0;
1760 return 0;
1761 }
1762
1763 #ifdef HAS_BAD144_HANDLING
1764 /*
1765 * Internalize the bad sector table.
1766 */
1767 void
1768 bad144intern(struct wd_softc *wd)
1769 {
1770 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1771 struct disklabel *lp = wd->sc_dk.dk_label;
1772 int i = 0;
1773
1774 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1775
1776 for (; i < NBT_BAD; i++) {
1777 if (bt->bt_bad[i].bt_cyl == 0xffff)
1778 break;
1779 wd->drvp->badsect[i] =
1780 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1781 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1782 (bt->bt_bad[i].bt_trksec & 0xff);
1783 }
1784 for (; i < NBT_BAD+1; i++)
1785 wd->drvp->badsect[i] = -1;
1786 }
1787 #endif
1788
1789 static void
1790 wd_params_to_properties(struct wd_softc *wd)
1791 {
1792 struct disk_geom *dg = &wd->sc_dk.dk_geom;
1793
1794 memset(dg, 0, sizeof(*dg));
1795
1796 dg->dg_secperunit = wd->sc_capacity;
1797 dg->dg_secsize = wd->sc_blksize;
1798 dg->dg_nsectors = wd->sc_params.atap_sectors;
1799 dg->dg_ntracks = wd->sc_params.atap_heads;
1800 if ((wd->sc_flags & WDF_LBA) == 0)
1801 dg->dg_ncylinders = wd->sc_params.atap_cylinders;
1802
1803 /* XXX Should have a case for ATA here, too. */
1804 const char *cp = strcmp(wd->sc_params.atap_model, "ST506") ?
1805 "ST506" : "ESDI";
1806
1807 disk_set_info(wd->sc_dev, &wd->sc_dk, cp);
1808 }
1809
1810 int
1811 wd_get_params(struct wd_softc *wd, uint8_t flags, struct ataparams *params)
1812 {
1813
1814 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1815 case CMD_AGAIN:
1816 return 1;
1817 case CMD_ERR:
1818 if (wd->drvp->drive_type != ATA_DRIVET_OLD)
1819 return 1;
1820 /*
1821 * We `know' there's a drive here; just assume it's old.
1822 * This geometry is only used to read the MBR and print a
1823 * (false) attach message.
1824 */
1825 strncpy(params->atap_model, "ST506",
1826 sizeof params->atap_model);
1827 params->atap_config = ATA_CFG_FIXED;
1828 params->atap_cylinders = 1024;
1829 params->atap_heads = 8;
1830 params->atap_sectors = 17;
1831 params->atap_multi = 1;
1832 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1833 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1834 /* FALLTHROUGH */
1835 case CMD_OK:
1836 return 0;
1837 default:
1838 panic("wd_get_params: bad return code from ata_get_params");
1839 /* NOTREACHED */
1840 }
1841 }
1842
1843 int
1844 wd_getcache(struct wd_softc *wd, int *bitsp)
1845 {
1846 struct ataparams params;
1847
1848 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1849 return EIO;
1850 if (params.atap_cmd_set1 == 0x0000 ||
1851 params.atap_cmd_set1 == 0xffff ||
1852 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1853 *bitsp = 0;
1854 return 0;
1855 }
1856 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1857 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1858 *bitsp |= DKCACHE_WRITE;
1859
1860 if (wd->drvp->drive_flags & (ATA_DRIVE_NCQ|ATA_DRIVE_WFUA))
1861 *bitsp |= DKCACHE_FUA;
1862
1863 return 0;
1864 }
1865
1866 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1867
1868 int
1869 wd_setcache(struct wd_softc *wd, int bits)
1870 {
1871 struct ataparams params;
1872 struct ata_xfer *xfer;
1873 int error;
1874
1875 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1876 return EIO;
1877
1878 if (params.atap_cmd_set1 == 0x0000 ||
1879 params.atap_cmd_set1 == 0xffff ||
1880 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1881 return EOPNOTSUPP;
1882
1883 if ((bits & DKCACHE_READ) == 0 ||
1884 (bits & DKCACHE_SAVE) != 0)
1885 return EOPNOTSUPP;
1886
1887 xfer = ata_get_xfer(wd->drvp->chnl_softc);
1888 if (xfer == NULL)
1889 return EINTR;
1890
1891 xfer->c_ata_c.r_command = SET_FEATURES;
1892 xfer->c_ata_c.r_st_bmask = 0;
1893 xfer->c_ata_c.r_st_pmask = 0;
1894 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
1895 xfer->c_ata_c.flags = AT_WAIT;
1896 if (bits & DKCACHE_WRITE)
1897 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_EN;
1898 else
1899 xfer->c_ata_c.r_features = WDSF_WRITE_CACHE_DS;
1900 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1901 aprint_error_dev(wd->sc_dev,
1902 "wd_setcache command not complete\n");
1903 error = EIO;
1904 goto out;
1905 }
1906
1907 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1908 char sbuf[sizeof(at_errbits) + 64];
1909 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1910 aprint_error_dev(wd->sc_dev, "wd_setcache: status=%s\n", sbuf);
1911 error = EIO;
1912 goto out;
1913 }
1914
1915 error = 0;
1916
1917 out:
1918 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1919 ata_channel_start(wd->drvp->chnl_softc, wd->drvp->drive);
1920 return error;
1921 }
1922
1923 static int
1924 wd_standby(struct wd_softc *wd, int flags)
1925 {
1926 struct ata_xfer *xfer;
1927 int error;
1928
1929 xfer = ata_get_xfer(wd->drvp->chnl_softc);
1930 if (xfer == NULL)
1931 return EINTR;
1932
1933 xfer->c_ata_c.r_command = WDCC_STANDBY_IMMED;
1934 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
1935 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
1936 xfer->c_ata_c.flags = flags;
1937 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
1938 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
1939 aprint_error_dev(wd->sc_dev,
1940 "standby immediate command didn't complete\n");
1941 error = EIO;
1942 goto out;
1943 }
1944 if (xfer->c_ata_c.flags & AT_ERROR) {
1945 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
1946 /* command not supported */
1947 error = ENODEV;
1948 goto out;
1949 }
1950 }
1951 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1952 char sbuf[sizeof(at_errbits) + 64];
1953 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
1954 aprint_error_dev(wd->sc_dev, "wd_standby: status=%s\n", sbuf);
1955 error = EIO;
1956 goto out;
1957 }
1958 error = 0;
1959
1960 out:
1961 ata_free_xfer(wd->drvp->chnl_softc, xfer);
1962 /* drive is supposed to go idle, do not call ata_channel_start() */
1963 return error;
1964 }
1965
1966 int
1967 wd_flushcache(struct wd_softc *wd, int flags, bool start)
1968 {
1969 struct ata_xfer *xfer;
1970 int error;
1971
1972 /*
1973 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1974 * only ATA-2 and still support it.
1975 */
1976 if (wd->drvp->ata_vers < 4 &&
1977 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1978 wd->sc_params.atap_cmd_set2 == 0xffff))
1979 return ENODEV;
1980
1981 mutex_enter(&wd->sc_lock);
1982 SET(wd->sc_flags, WDF_FLUSH_PEND);
1983 mutex_exit(&wd->sc_lock);
1984
1985 xfer = ata_get_xfer(wd->drvp->chnl_softc);
1986
1987 mutex_enter(&wd->sc_lock);
1988 CLR(wd->sc_flags, WDF_FLUSH_PEND);
1989 mutex_exit(&wd->sc_lock);
1990
1991 if (xfer == NULL) {
1992 error = EINTR;
1993 goto out;
1994 }
1995
1996 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1997 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0) {
1998 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1999 flags |= AT_LBA48;
2000 } else
2001 xfer->c_ata_c.r_command = WDCC_FLUSHCACHE;
2002 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
2003 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
2004 xfer->c_ata_c.flags = flags | AT_READREG;
2005 xfer->c_ata_c.timeout = 300000; /* 5m timeout */
2006 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
2007 aprint_error_dev(wd->sc_dev,
2008 "flush cache command didn't complete\n");
2009 error = EIO;
2010 goto out_xfer;
2011 }
2012 if (xfer->c_ata_c.flags & AT_ERROR) {
2013 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
2014 /* command not supported */
2015 error = ENODEV;
2016 goto out_xfer;
2017 }
2018 }
2019 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2020 char sbuf[sizeof(at_errbits) + 64];
2021 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
2022 aprint_error_dev(wd->sc_dev, "wd_flushcache: status=%s\n",
2023 sbuf);
2024 error = EIO;
2025 goto out_xfer;
2026 }
2027 error = 0;
2028
2029 out_xfer:
2030 ata_free_xfer(wd->drvp->chnl_softc, xfer);
2031
2032 out:
2033 /* kick queue processing blocked while waiting for flush xfer */
2034 if (start)
2035 ata_channel_start(wd->drvp->chnl_softc, wd->drvp->drive);
2036
2037 return error;
2038 }
2039
2040 int
2041 wd_trim(struct wd_softc *wd, int part, daddr_t bno, long size)
2042 {
2043 struct ata_xfer *xfer;
2044 int error;
2045 unsigned char *req;
2046
2047 if (part != RAW_PART)
2048 bno += wd->sc_dk.dk_label->d_partitions[part].p_offset;;
2049
2050 xfer = ata_get_xfer(wd->drvp->chnl_softc);
2051 if (xfer == NULL)
2052 return EINTR;
2053
2054 req = kmem_zalloc(512, KM_SLEEP);
2055 req[0] = bno & 0xff;
2056 req[1] = (bno >> 8) & 0xff;
2057 req[2] = (bno >> 16) & 0xff;
2058 req[3] = (bno >> 24) & 0xff;
2059 req[4] = (bno >> 32) & 0xff;
2060 req[5] = (bno >> 40) & 0xff;
2061 req[6] = size & 0xff;
2062 req[7] = (size >> 8) & 0xff;
2063
2064 xfer->c_ata_c.r_command = ATA_DATA_SET_MANAGEMENT;
2065 xfer->c_ata_c.r_count = 1;
2066 xfer->c_ata_c.r_features = ATA_SUPPORT_DSM_TRIM;
2067 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
2068 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
2069 xfer->c_ata_c.timeout = 30000; /* 30s timeout */
2070 xfer->c_ata_c.data = req;
2071 xfer->c_ata_c.bcount = 512;
2072 xfer->c_ata_c.flags |= AT_WRITE | AT_WAIT;
2073 if (wd->atabus->ata_exec_command(wd->drvp, xfer) != ATACMD_COMPLETE) {
2074 aprint_error_dev(wd->sc_dev,
2075 "trim command didn't complete\n");
2076 kmem_free(req, 512);
2077 error = EIO;
2078 goto out;
2079 }
2080 kmem_free(req, 512);
2081 if (xfer->c_ata_c.flags & AT_ERROR) {
2082 if (xfer->c_ata_c.r_error == WDCE_ABRT) {
2083 /* command not supported */
2084 error = ENODEV;
2085 goto out;
2086 }
2087 }
2088 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2089 char sbuf[sizeof(at_errbits) + 64];
2090 snprintb(sbuf, sizeof(sbuf), at_errbits, xfer->c_ata_c.flags);
2091 aprint_error_dev(wd->sc_dev, "wd_trim: status=%s\n",
2092 sbuf);
2093 error = EIO;
2094 goto out;
2095 }
2096 error = 0;
2097
2098 out:
2099 ata_free_xfer(wd->drvp->chnl_softc, xfer);
2100 ata_channel_start(wd->drvp->chnl_softc, wd->drvp->drive);
2101 return error;
2102 }
2103
2104 bool
2105 wd_shutdown(device_t dev, int how)
2106 {
2107 struct wd_softc *wd = device_private(dev);
2108
2109 /* the adapter needs to be enabled */
2110 if (wd->atabus->ata_addref(wd->drvp))
2111 return true; /* no need to complain */
2112
2113 wd_flushcache(wd, AT_POLL, false);
2114 if ((how & RB_POWERDOWN) == RB_POWERDOWN)
2115 wd_standby(wd, AT_POLL);
2116 return true;
2117 }
2118
2119 /*
2120 * Allocate space for a ioctl queue structure. Mostly taken from
2121 * scsipi_ioctl.c
2122 */
2123 struct wd_ioctl *
2124 wi_get(struct wd_softc *wd)
2125 {
2126 struct wd_ioctl *wi;
2127
2128 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
2129 wi->wi_softc = wd;
2130 buf_init(&wi->wi_bp);
2131
2132 return (wi);
2133 }
2134
2135 /*
2136 * Free an ioctl structure and remove it from our list
2137 */
2138
2139 void
2140 wi_free(struct wd_ioctl *wi)
2141 {
2142 buf_destroy(&wi->wi_bp);
2143 free(wi, M_TEMP);
2144 }
2145
2146 /*
2147 * Find a wd_ioctl structure based on the struct buf.
2148 */
2149
2150 struct wd_ioctl *
2151 wi_find(struct buf *bp)
2152 {
2153 return container_of(bp, struct wd_ioctl, wi_bp);
2154 }
2155
2156 static uint
2157 wi_sector_size(const struct wd_ioctl * const wi)
2158 {
2159 switch (wi->wi_atareq.command) {
2160 case WDCC_READ:
2161 case WDCC_WRITE:
2162 case WDCC_READMULTI:
2163 case WDCC_WRITEMULTI:
2164 case WDCC_READDMA:
2165 case WDCC_WRITEDMA:
2166 case WDCC_READ_EXT:
2167 case WDCC_WRITE_EXT:
2168 case WDCC_READMULTI_EXT:
2169 case WDCC_WRITEMULTI_EXT:
2170 case WDCC_READDMA_EXT:
2171 case WDCC_WRITEDMA_EXT:
2172 case WDCC_READ_FPDMA_QUEUED:
2173 case WDCC_WRITE_FPDMA_QUEUED:
2174 return wi->wi_softc->sc_blksize;
2175 default:
2176 return 512;
2177 }
2178 }
2179
2180 /*
2181 * Ioctl pseudo strategy routine
2182 *
2183 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
2184 * happens here is:
2185 *
2186 * - wdioctl() queues a wd_ioctl structure.
2187 *
2188 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
2189 * user space I/O is required. If physio() is called, physio() eventually
2190 * calls wdioctlstrategy().
2191 *
2192 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
2193 * to perform the actual command
2194 *
2195 * The reason for the use of the pseudo strategy routine is because
2196 * when doing I/O to/from user space, physio _really_ wants to be in
2197 * the loop. We could put the entire buffer into the ioctl request
2198 * structure, but that won't scale if we want to do things like download
2199 * microcode.
2200 */
2201
2202 void
2203 wdioctlstrategy(struct buf *bp)
2204 {
2205 struct wd_ioctl *wi;
2206 struct ata_xfer *xfer;
2207 int error = 0;
2208
2209 wi = wi_find(bp);
2210 if (wi == NULL) {
2211 printf("wdioctlstrategy: "
2212 "No matching ioctl request found in queue\n");
2213 error = EINVAL;
2214 goto out2;
2215 }
2216
2217 xfer = ata_get_xfer(wi->wi_softc->drvp->chnl_softc);
2218 if (xfer == NULL) {
2219 error = EINTR;
2220 goto out2;
2221 }
2222
2223 /*
2224 * Abort if physio broke up the transfer
2225 */
2226
2227 if (bp->b_bcount != wi->wi_atareq.datalen) {
2228 printf("physio split wd ioctl request... cannot proceed\n");
2229 error = EIO;
2230 goto out;
2231 }
2232
2233 /*
2234 * Abort if we didn't get a buffer size that was a multiple of
2235 * our sector size (or overflows CHS/LBA28 sector count)
2236 */
2237
2238 if ((bp->b_bcount % wi_sector_size(wi)) != 0 ||
2239 (bp->b_bcount / wi_sector_size(wi)) >=
2240 (1 << NBBY)) {
2241 error = EINVAL;
2242 goto out;
2243 }
2244
2245 /*
2246 * Make sure a timeout was supplied in the ioctl request
2247 */
2248
2249 if (wi->wi_atareq.timeout == 0) {
2250 error = EINVAL;
2251 goto out;
2252 }
2253
2254 if (wi->wi_atareq.flags & ATACMD_READ)
2255 xfer->c_ata_c.flags |= AT_READ;
2256 else if (wi->wi_atareq.flags & ATACMD_WRITE)
2257 xfer->c_ata_c.flags |= AT_WRITE;
2258
2259 if (wi->wi_atareq.flags & ATACMD_READREG)
2260 xfer->c_ata_c.flags |= AT_READREG;
2261
2262 if ((wi->wi_atareq.flags & ATACMD_LBA) != 0)
2263 xfer->c_ata_c.flags |= AT_LBA;
2264
2265 xfer->c_ata_c.flags |= AT_WAIT;
2266
2267 xfer->c_ata_c.timeout = wi->wi_atareq.timeout;
2268 xfer->c_ata_c.r_command = wi->wi_atareq.command;
2269 xfer->c_ata_c.r_lba = ((wi->wi_atareq.head & 0x0f) << 24) |
2270 (wi->wi_atareq.cylinder << 8) |
2271 wi->wi_atareq.sec_num;
2272 xfer->c_ata_c.r_count = wi->wi_atareq.sec_count;
2273 xfer->c_ata_c.r_features = wi->wi_atareq.features;
2274 xfer->c_ata_c.r_st_bmask = WDCS_DRDY;
2275 xfer->c_ata_c.r_st_pmask = WDCS_DRDY;
2276 xfer->c_ata_c.data = wi->wi_bp.b_data;
2277 xfer->c_ata_c.bcount = wi->wi_bp.b_bcount;
2278
2279 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, xfer)
2280 != ATACMD_COMPLETE) {
2281 wi->wi_atareq.retsts = ATACMD_ERROR;
2282 error = EIO;
2283 goto out;
2284 }
2285
2286 if (xfer->c_ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
2287 if (xfer->c_ata_c.flags & AT_ERROR) {
2288 wi->wi_atareq.retsts = ATACMD_ERROR;
2289 wi->wi_atareq.error = xfer->c_ata_c.r_error;
2290 } else if (xfer->c_ata_c.flags & AT_DF)
2291 wi->wi_atareq.retsts = ATACMD_DF;
2292 else
2293 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
2294 } else {
2295 wi->wi_atareq.retsts = ATACMD_OK;
2296 if (wi->wi_atareq.flags & ATACMD_READREG) {
2297 wi->wi_atareq.command = xfer->c_ata_c.r_status;
2298 wi->wi_atareq.features = xfer->c_ata_c.r_error;
2299 wi->wi_atareq.sec_count = xfer->c_ata_c.r_count;
2300 wi->wi_atareq.sec_num = xfer->c_ata_c.r_lba & 0xff;
2301 wi->wi_atareq.head = (xfer->c_ata_c.r_device & 0xf0) |
2302 ((xfer->c_ata_c.r_lba >> 24) & 0x0f);
2303 wi->wi_atareq.cylinder =
2304 (xfer->c_ata_c.r_lba >> 8) & 0xffff;
2305 wi->wi_atareq.error = xfer->c_ata_c.r_error;
2306 }
2307 }
2308
2309 out:
2310 ata_free_xfer(wi->wi_softc->drvp->chnl_softc, xfer);
2311 ata_channel_start(wi->wi_softc->drvp->chnl_softc,
2312 wi->wi_softc->drvp->drive);
2313 out2:
2314 bp->b_error = error;
2315 if (error)
2316 bp->b_resid = bp->b_bcount;
2317 biodone(bp);
2318 }
2319