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