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