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