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