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