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