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