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