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