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