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