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