wd.c revision 1.324 1 /* $NetBSD: wd.c,v 1.324 2006/05/18 19:42:09 bouyer Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 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.324 2006/05/18 19:42:09 bouyer 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 { "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*",
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 tbuf[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 = tbuf, 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", tbuf);
335
336 wdq = wd_lookup_quirks(tbuf);
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(device_unit(self), 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_ERROR;
651 obp->b_error = bp->b_error;
652 obp->b_resid = bp->b_resid;
653 putiobuf(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 = getiobuf_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 nbp->b_error = 0;
690 nbp->b_proc = bp->b_proc;
691 nbp->b_vp = NULLVP;
692 nbp->b_dev = bp->b_dev;
693
694 nbp->b_bcount = bp->b_bcount / 2;
695 nbp->b_bufsize = bp->b_bcount / 2;
696 nbp->b_data = bp->b_data;
697
698 nbp->b_blkno = bp->b_blkno;
699 nbp->b_rawblkno = bp->b_rawblkno;
700
701 nbp->b_flags = bp->b_flags | B_CALL;
702 nbp->b_iodone = wd_split_mod15_write;
703
704 /* Put ptr to orig buf in b_private and use new buf */
705 nbp->b_private = bp;
706
707 BIO_COPYPRIO(nbp, bp);
708
709 bp = nbp;
710 }
711
712 wd->sc_wdc_bio.blkno = bp->b_rawblkno;
713 wd->sc_wdc_bio.blkdone =0;
714 wd->sc_bp = bp;
715 /*
716 * If we're retrying, retry in single-sector mode. This will give us
717 * the sector number of the problem, and will eventually allow the
718 * transfer to succeed.
719 */
720 if (wd->retries >= WDIORETRIES_SINGLE)
721 wd->sc_wdc_bio.flags = ATA_SINGLE;
722 else
723 wd->sc_wdc_bio.flags = 0;
724 if (wd->sc_flags & WDF_LBA48 &&
725 (wd->sc_wdc_bio.blkno > LBA48_THRESHOLD ||
726 (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
727 wd->sc_wdc_bio.flags |= ATA_LBA48;
728 if (wd->sc_flags & WDF_LBA)
729 wd->sc_wdc_bio.flags |= ATA_LBA;
730 if (bp->b_flags & B_READ)
731 wd->sc_wdc_bio.flags |= ATA_READ;
732 wd->sc_wdc_bio.bcount = bp->b_bcount;
733 wd->sc_wdc_bio.databuf = bp->b_data;
734 /* Instrumentation. */
735 disk_busy(&wd->sc_dk);
736 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
737 case ATACMD_TRY_AGAIN:
738 callout_reset(&wd->sc_restart_ch, hz, wdrestart, wd);
739 break;
740 case ATACMD_QUEUED:
741 case ATACMD_COMPLETE:
742 break;
743 default:
744 panic("__wdstart: bad return code from ata_bio()");
745 }
746 }
747
748 void
749 wddone(void *v)
750 {
751 struct wd_softc *wd = v;
752 struct buf *bp = wd->sc_bp;
753 const char *errmsg;
754 int do_perror = 0;
755 ATADEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
756 DEBUG_XFERS);
757
758 if (bp == NULL)
759 return;
760 bp->b_resid = wd->sc_wdc_bio.bcount;
761 switch (wd->sc_wdc_bio.error) {
762 case ERR_DMA:
763 errmsg = "DMA error";
764 goto retry;
765 case ERR_DF:
766 errmsg = "device fault";
767 goto retry;
768 case TIMEOUT:
769 errmsg = "device timeout";
770 goto retry;
771 case ERR_RESET:
772 errmsg = "channel reset";
773 goto retry2;
774 case ERROR:
775 /* Don't care about media change bits */
776 if (wd->sc_wdc_bio.r_error != 0 &&
777 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
778 goto noerror;
779 errmsg = "error";
780 do_perror = 1;
781 retry: /* Just reset and retry. Can we do more ? */
782 (*wd->atabus->ata_reset_drive)(wd->drvp, AT_RST_NOCMD);
783 retry2:
784 diskerr(bp, "wd", errmsg, LOG_PRINTF,
785 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
786 if (wd->retries < WDIORETRIES)
787 printf(", retrying\n");
788 if (do_perror)
789 wdperror(wd);
790 if (wd->retries < WDIORETRIES) {
791 wd->retries++;
792 callout_reset(&wd->sc_restart_ch, RECOVERYTIME,
793 wdrestart, wd);
794 return;
795 }
796 printf("\n");
797
798 #ifdef WD_SOFTBADSECT
799 /*
800 * Not all errors indicate a failed block but those that do,
801 * put the block on the bad-block list for the device. Only
802 * do this for reads because the drive should do it for writes,
803 * itself, according to Manuel.
804 */
805 if ((bp->b_flags & B_READ) &&
806 ((wd->drvp->ata_vers >= 4 && wd->sc_wdc_bio.r_error & 64) ||
807 (wd->drvp->ata_vers < 4 && wd->sc_wdc_bio.r_error & 192))) {
808 struct disk_badsectors *dbs;
809
810 dbs = malloc(sizeof *dbs, M_TEMP, M_WAITOK);
811 dbs->dbs_min = bp->b_rawblkno;
812 dbs->dbs_max = dbs->dbs_min + (bp->b_bcount >> DEV_BSHIFT) - 1;
813 microtime(&dbs->dbs_failedat);
814 SLIST_INSERT_HEAD(&wd->sc_bslist, dbs, dbs_next);
815 wd->sc_bscount++;
816 }
817 #endif
818 bp->b_flags |= B_ERROR;
819 bp->b_error = EIO;
820 break;
821 case NOERROR:
822 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
823 printf("%s: soft error (corrected)\n",
824 wd->sc_dev.dv_xname);
825 break;
826 case ERR_NODEV:
827 bp->b_flags |= B_ERROR;
828 bp->b_error = EIO;
829 break;
830 }
831 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid),
832 (bp->b_flags & B_READ));
833 #if NRND > 0
834 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
835 #endif
836 /* XXX Yuck, but we don't want to increment openings in this case */
837 if (__predict_false((bp->b_flags & B_CALL) != 0 &&
838 bp->b_iodone == wd_split_mod15_write))
839 biodone(bp);
840 else {
841 biodone(bp);
842 wd->openings++;
843 }
844 wdstart(wd);
845 }
846
847 void
848 wdrestart(void *v)
849 {
850 struct wd_softc *wd = v;
851 struct buf *bp = wd->sc_bp;
852 int s;
853 ATADEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
854 DEBUG_XFERS);
855
856 s = splbio();
857 __wdstart(v, bp);
858 splx(s);
859 }
860
861 int
862 wdread(dev_t dev, struct uio *uio, int flags)
863 {
864
865 ATADEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
866 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
867 }
868
869 int
870 wdwrite(dev_t dev, struct uio *uio, int flags)
871 {
872
873 ATADEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
874 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
875 }
876
877 int
878 wdopen(dev_t dev, int flag, int fmt, struct lwp *l)
879 {
880 struct wd_softc *wd;
881 int part, error;
882
883 ATADEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
884 wd = device_lookup(&wd_cd, WDUNIT(dev));
885 if (wd == NULL)
886 return (ENXIO);
887
888 if (! device_is_active(&wd->sc_dev))
889 return (ENODEV);
890
891 part = WDPART(dev);
892
893 if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
894 return (error);
895
896 /*
897 * If there are wedges, and this is not RAW_PART, then we
898 * need to fail.
899 */
900 if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
901 error = EBUSY;
902 goto bad1;
903 }
904
905 /*
906 * If this is the first open of this device, add a reference
907 * to the adapter.
908 */
909 if (wd->sc_dk.dk_openmask == 0 &&
910 (error = wd->atabus->ata_addref(wd->drvp)) != 0)
911 goto bad1;
912
913 if (wd->sc_dk.dk_openmask != 0) {
914 /*
915 * If any partition is open, but the disk has been invalidated,
916 * disallow further opens.
917 */
918 if ((wd->sc_flags & WDF_LOADED) == 0) {
919 error = EIO;
920 goto bad2;
921 }
922 } else {
923 if ((wd->sc_flags & WDF_LOADED) == 0) {
924 wd->sc_flags |= WDF_LOADED;
925
926 /* Load the physical device parameters. */
927 wd_get_params(wd, AT_WAIT, &wd->sc_params);
928
929 /* Load the partition info if not already loaded. */
930 wdgetdisklabel(wd);
931 }
932 }
933
934 /* Check that the partition exists. */
935 if (part != RAW_PART &&
936 (part >= wd->sc_dk.dk_label->d_npartitions ||
937 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
938 error = ENXIO;
939 goto bad2;
940 }
941
942 /* Insure only one open at a time. */
943 switch (fmt) {
944 case S_IFCHR:
945 wd->sc_dk.dk_copenmask |= (1 << part);
946 break;
947 case S_IFBLK:
948 wd->sc_dk.dk_bopenmask |= (1 << part);
949 break;
950 }
951 wd->sc_dk.dk_openmask =
952 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
953
954 (void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
955 return 0;
956
957 bad2:
958 if (wd->sc_dk.dk_openmask == 0)
959 wd->atabus->ata_delref(wd->drvp);
960 bad1:
961 (void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
962 return error;
963 }
964
965 int
966 wdclose(dev_t dev, int flag, int fmt, struct lwp *l)
967 {
968 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
969 int part = WDPART(dev);
970 int error;
971
972 ATADEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
973
974 if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
975 return error;
976
977 switch (fmt) {
978 case S_IFCHR:
979 wd->sc_dk.dk_copenmask &= ~(1 << part);
980 break;
981 case S_IFBLK:
982 wd->sc_dk.dk_bopenmask &= ~(1 << part);
983 break;
984 }
985 wd->sc_dk.dk_openmask =
986 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
987
988 if (wd->sc_dk.dk_openmask == 0) {
989 wd_flushcache(wd, AT_WAIT);
990
991 if (! (wd->sc_flags & WDF_KLABEL))
992 wd->sc_flags &= ~WDF_LOADED;
993
994 wd->atabus->ata_delref(wd->drvp);
995 }
996
997 (void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
998 return 0;
999 }
1000
1001 void
1002 wdgetdefaultlabel(struct wd_softc *wd, struct disklabel *lp)
1003 {
1004
1005 ATADEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
1006 memset(lp, 0, sizeof(struct disklabel));
1007
1008 lp->d_secsize = DEV_BSIZE;
1009 lp->d_ntracks = wd->sc_params.atap_heads;
1010 lp->d_nsectors = wd->sc_params.atap_sectors;
1011 lp->d_ncylinders = (wd->sc_flags & WDF_LBA) ? wd->sc_capacity /
1012 (wd->sc_params.atap_heads * wd->sc_params.atap_sectors) :
1013 wd->sc_params.atap_cylinders;
1014 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
1015
1016 if (strcmp(wd->sc_params.atap_model, "ST506") == 0)
1017 lp->d_type = DTYPE_ST506;
1018 else
1019 lp->d_type = DTYPE_ESDI;
1020
1021 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
1022 strncpy(lp->d_packname, "fictitious", 16);
1023 if (wd->sc_capacity > UINT32_MAX)
1024 lp->d_secperunit = UINT32_MAX;
1025 else
1026 lp->d_secperunit = wd->sc_capacity;
1027 lp->d_rpm = 3600;
1028 lp->d_interleave = 1;
1029 lp->d_flags = 0;
1030
1031 lp->d_partitions[RAW_PART].p_offset = 0;
1032 lp->d_partitions[RAW_PART].p_size =
1033 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
1034 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
1035 lp->d_npartitions = RAW_PART + 1;
1036
1037 lp->d_magic = DISKMAGIC;
1038 lp->d_magic2 = DISKMAGIC;
1039 lp->d_checksum = dkcksum(lp);
1040 }
1041
1042 /*
1043 * Fabricate a default disk label, and try to read the correct one.
1044 */
1045 void
1046 wdgetdisklabel(struct wd_softc *wd)
1047 {
1048 struct disklabel *lp = wd->sc_dk.dk_label;
1049 const char *errstring;
1050 int s;
1051
1052 ATADEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
1053
1054 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
1055
1056 wdgetdefaultlabel(wd, lp);
1057
1058 wd->sc_badsect[0] = -1;
1059
1060 if (wd->drvp->state > RESET) {
1061 s = splbio();
1062 wd->drvp->drive_flags |= DRIVE_RESET;
1063 splx(s);
1064 }
1065 errstring = readdisklabel(MAKEWDDEV(0, device_unit(&wd->sc_dev),
1066 RAW_PART), wdstrategy, lp,
1067 wd->sc_dk.dk_cpulabel);
1068 if (errstring) {
1069 /*
1070 * This probably happened because the drive's default
1071 * geometry doesn't match the DOS geometry. We
1072 * assume the DOS geometry is now in the label and try
1073 * again. XXX This is a kluge.
1074 */
1075 if (wd->drvp->state > RESET) {
1076 s = splbio();
1077 wd->drvp->drive_flags |= DRIVE_RESET;
1078 splx(s);
1079 }
1080 errstring = readdisklabel(MAKEWDDEV(0, device_unit(&wd->sc_dev),
1081 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
1082 }
1083 if (errstring) {
1084 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
1085 return;
1086 }
1087
1088 if (wd->drvp->state > RESET) {
1089 s = splbio();
1090 wd->drvp->drive_flags |= DRIVE_RESET;
1091 splx(s);
1092 }
1093 #ifdef HAS_BAD144_HANDLING
1094 if ((lp->d_flags & D_BADSECT) != 0)
1095 bad144intern(wd);
1096 #endif
1097 }
1098
1099 void
1100 wdperror(const struct wd_softc *wd)
1101 {
1102 static const char *const errstr0_3[] = {"address mark not found",
1103 "track 0 not found", "aborted command", "media change requested",
1104 "id not found", "media changed", "uncorrectable data error",
1105 "bad block detected"};
1106 static const char *const errstr4_5[] = {
1107 "obsolete (address mark not found)",
1108 "no media/write protected", "aborted command",
1109 "media change requested", "id not found", "media changed",
1110 "uncorrectable data error", "interface CRC error"};
1111 const char *const *errstr;
1112 int i;
1113 const char *sep = "";
1114
1115 const char *devname = wd->sc_dev.dv_xname;
1116 struct ata_drive_datas *drvp = wd->drvp;
1117 int errno = wd->sc_wdc_bio.r_error;
1118
1119 if (drvp->ata_vers >= 4)
1120 errstr = errstr4_5;
1121 else
1122 errstr = errstr0_3;
1123
1124 printf("%s: (", devname);
1125
1126 if (errno == 0)
1127 printf("error not notified");
1128
1129 for (i = 0; i < 8; i++) {
1130 if (errno & (1 << i)) {
1131 printf("%s%s", sep, errstr[i]);
1132 sep = ", ";
1133 }
1134 }
1135 printf(")\n");
1136 }
1137
1138 int
1139 wdioctl(dev_t dev, u_long xfer, caddr_t addr, int flag, struct lwp *l)
1140 {
1141 struct wd_softc *wd = device_lookup(&wd_cd, WDUNIT(dev));
1142 int error = 0, s;
1143 #ifdef __HAVE_OLD_DISKLABEL
1144 struct disklabel *newlabel = NULL;
1145 #endif
1146
1147 ATADEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
1148
1149 if ((wd->sc_flags & WDF_LOADED) == 0)
1150 return EIO;
1151
1152 switch (xfer) {
1153 #ifdef HAS_BAD144_HANDLING
1154 case DIOCSBAD:
1155 if ((flag & FWRITE) == 0)
1156 return EBADF;
1157 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
1158 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
1159 bad144intern(wd);
1160 return 0;
1161 #endif
1162 #ifdef WD_SOFTBADSECT
1163 case DIOCBSLIST :
1164 {
1165 u_int32_t count, missing, skip;
1166 struct disk_badsecinfo dbsi;
1167 struct disk_badsectors *dbs;
1168 size_t available;
1169 caddr_t laddr;
1170
1171 dbsi = *(struct disk_badsecinfo *)addr;
1172 missing = wd->sc_bscount;
1173 count = 0;
1174 available = dbsi.dbsi_bufsize;
1175 skip = dbsi.dbsi_skip;
1176 laddr = dbsi.dbsi_buffer;
1177
1178 /*
1179 * We start this loop with the expectation that all of the
1180 * entries will be missed and decrement this counter each
1181 * time we either skip over one (already copied out) or
1182 * we actually copy it back to user space. The structs
1183 * holding the bad sector information are copied directly
1184 * back to user space whilst the summary is returned via
1185 * the struct passed in via the ioctl.
1186 */
1187 SLIST_FOREACH(dbs, &wd->sc_bslist, dbs_next) {
1188 if (skip > 0) {
1189 missing--;
1190 skip--;
1191 continue;
1192 }
1193 if (available < sizeof(*dbs))
1194 break;
1195 available -= sizeof(*dbs);
1196 copyout(dbs, laddr, sizeof(*dbs));
1197 laddr += sizeof(*dbs);
1198 missing--;
1199 count++;
1200 }
1201 dbsi.dbsi_left = missing;
1202 dbsi.dbsi_copied = count;
1203 *(struct disk_badsecinfo *)addr = dbsi;
1204 return 0;
1205 }
1206
1207 case DIOCBSFLUSH :
1208 /* Clean out the bad sector list */
1209 while (!SLIST_EMPTY(&wd->sc_bslist)) {
1210 void *head = SLIST_FIRST(&wd->sc_bslist);
1211 SLIST_REMOVE_HEAD(&wd->sc_bslist, dbs_next);
1212 free(head, M_TEMP);
1213 }
1214 wd->sc_bscount = 0;
1215 return 0;
1216 #endif
1217 case DIOCGDINFO:
1218 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
1219 return 0;
1220 #ifdef __HAVE_OLD_DISKLABEL
1221 case ODIOCGDINFO:
1222 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1223 if (newlabel == NULL)
1224 return EIO;
1225 *newlabel = *(wd->sc_dk.dk_label);
1226 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1227 memcpy(addr, newlabel, sizeof (struct olddisklabel));
1228 else
1229 error = ENOTTY;
1230 free(newlabel, M_TEMP);
1231 return error;
1232 #endif
1233
1234 case DIOCGPART:
1235 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
1236 ((struct partinfo *)addr)->part =
1237 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
1238 return 0;
1239
1240 case DIOCWDINFO:
1241 case DIOCSDINFO:
1242 #ifdef __HAVE_OLD_DISKLABEL
1243 case ODIOCWDINFO:
1244 case ODIOCSDINFO:
1245 #endif
1246 {
1247 struct disklabel *lp;
1248
1249 if ((flag & FWRITE) == 0)
1250 return EBADF;
1251
1252 #ifdef __HAVE_OLD_DISKLABEL
1253 if (xfer == ODIOCSDINFO || xfer == ODIOCWDINFO) {
1254 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1255 if (newlabel == NULL)
1256 return EIO;
1257 memset(newlabel, 0, sizeof newlabel);
1258 memcpy(newlabel, addr, sizeof (struct olddisklabel));
1259 lp = newlabel;
1260 } else
1261 #endif
1262 lp = (struct disklabel *)addr;
1263
1264 if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE,
1265 NULL)) != 0)
1266 goto bad;
1267 wd->sc_flags |= WDF_LABELLING;
1268
1269 error = setdisklabel(wd->sc_dk.dk_label,
1270 lp, /*wd->sc_dk.dk_openmask : */0,
1271 wd->sc_dk.dk_cpulabel);
1272 if (error == 0) {
1273 if (wd->drvp->state > RESET) {
1274 s = splbio();
1275 wd->drvp->drive_flags |= DRIVE_RESET;
1276 splx(s);
1277 }
1278 if (xfer == DIOCWDINFO
1279 #ifdef __HAVE_OLD_DISKLABEL
1280 || xfer == ODIOCWDINFO
1281 #endif
1282 )
1283 error = writedisklabel(WDLABELDEV(dev),
1284 wdstrategy, wd->sc_dk.dk_label,
1285 wd->sc_dk.dk_cpulabel);
1286 }
1287
1288 wd->sc_flags &= ~WDF_LABELLING;
1289 (void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
1290 bad:
1291 #ifdef __HAVE_OLD_DISKLABEL
1292 if (newlabel != NULL)
1293 free(newlabel, M_TEMP);
1294 #endif
1295 return error;
1296 }
1297
1298 case DIOCKLABEL:
1299 if (*(int *)addr)
1300 wd->sc_flags |= WDF_KLABEL;
1301 else
1302 wd->sc_flags &= ~WDF_KLABEL;
1303 return 0;
1304
1305 case DIOCWLABEL:
1306 if ((flag & FWRITE) == 0)
1307 return EBADF;
1308 if (*(int *)addr)
1309 wd->sc_flags |= WDF_WLABEL;
1310 else
1311 wd->sc_flags &= ~WDF_WLABEL;
1312 return 0;
1313
1314 case DIOCGDEFLABEL:
1315 wdgetdefaultlabel(wd, (struct disklabel *)addr);
1316 return 0;
1317 #ifdef __HAVE_OLD_DISKLABEL
1318 case ODIOCGDEFLABEL:
1319 newlabel = malloc(sizeof *newlabel, M_TEMP, M_WAITOK);
1320 if (newlabel == NULL)
1321 return EIO;
1322 wdgetdefaultlabel(wd, newlabel);
1323 if (newlabel->d_npartitions <= OLDMAXPARTITIONS)
1324 memcpy(addr, &newlabel, sizeof (struct olddisklabel));
1325 else
1326 error = ENOTTY;
1327 free(newlabel, M_TEMP);
1328 return error;
1329 #endif
1330
1331 #ifdef notyet
1332 case DIOCWFORMAT:
1333 if ((flag & FWRITE) == 0)
1334 return EBADF;
1335 {
1336 register struct format_op *fop;
1337 struct iovec aiov;
1338 struct uio auio;
1339
1340 fop = (struct format_op *)addr;
1341 aiov.iov_base = fop->df_buf;
1342 aiov.iov_len = fop->df_count;
1343 auio.uio_iov = &aiov;
1344 auio.uio_iovcnt = 1;
1345 auio.uio_resid = fop->df_count;
1346 auio.uio_offset =
1347 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
1348 auio.uio_vmspace = l->l_proc->p_vmspace;
1349 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
1350 &auio);
1351 fop->df_count -= auio.uio_resid;
1352 fop->df_reg[0] = wdc->sc_status;
1353 fop->df_reg[1] = wdc->sc_error;
1354 return error;
1355 }
1356 #endif
1357 case DIOCGCACHE:
1358 return wd_getcache(wd, (int *)addr);
1359
1360 case DIOCSCACHE:
1361 return wd_setcache(wd, *(int *)addr);
1362
1363 case DIOCCACHESYNC:
1364 return wd_flushcache(wd, AT_WAIT);
1365
1366 case ATAIOCCOMMAND:
1367 /*
1368 * Make sure this command is (relatively) safe first
1369 */
1370 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
1371 (flag & FWRITE) == 0)
1372 return (EBADF);
1373 {
1374 struct wd_ioctl *wi;
1375 atareq_t *atareq = (atareq_t *) addr;
1376 int error1;
1377
1378 wi = wi_get();
1379 wi->wi_softc = wd;
1380 wi->wi_atareq = *atareq;
1381
1382 if (atareq->datalen && atareq->flags &
1383 (ATACMD_READ | ATACMD_WRITE)) {
1384 wi->wi_iov.iov_base = atareq->databuf;
1385 wi->wi_iov.iov_len = atareq->datalen;
1386 wi->wi_uio.uio_iov = &wi->wi_iov;
1387 wi->wi_uio.uio_iovcnt = 1;
1388 wi->wi_uio.uio_resid = atareq->datalen;
1389 wi->wi_uio.uio_offset = 0;
1390 wi->wi_uio.uio_rw =
1391 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
1392 wi->wi_uio.uio_vmspace = l->l_proc->p_vmspace;
1393 error1 = physio(wdioctlstrategy, &wi->wi_bp, dev,
1394 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
1395 minphys, &wi->wi_uio);
1396 } else {
1397 /* No need to call physio if we don't have any
1398 user data */
1399 wi->wi_bp.b_flags = 0;
1400 wi->wi_bp.b_data = 0;
1401 wi->wi_bp.b_bcount = 0;
1402 wi->wi_bp.b_dev = 0;
1403 wi->wi_bp.b_proc = l->l_proc;
1404 wdioctlstrategy(&wi->wi_bp);
1405 error1 = wi->wi_bp.b_error;
1406 }
1407 *atareq = wi->wi_atareq;
1408 wi_free(wi);
1409 return(error1);
1410 }
1411
1412 case DIOCAWEDGE:
1413 {
1414 struct dkwedge_info *dkw = (void *) addr;
1415
1416 if ((flag & FWRITE) == 0)
1417 return (EBADF);
1418
1419 /* If the ioctl happens here, the parent is us. */
1420 strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
1421 return (dkwedge_add(dkw));
1422 }
1423
1424 case DIOCDWEDGE:
1425 {
1426 struct dkwedge_info *dkw = (void *) addr;
1427
1428 if ((flag & FWRITE) == 0)
1429 return (EBADF);
1430
1431 /* If the ioctl happens here, the parent is us. */
1432 strcpy(dkw->dkw_parent, wd->sc_dev.dv_xname);
1433 return (dkwedge_del(dkw));
1434 }
1435
1436 case DIOCLWEDGES:
1437 {
1438 struct dkwedge_list *dkwl = (void *) addr;
1439
1440 return (dkwedge_list(&wd->sc_dk, dkwl, l));
1441 }
1442
1443 case DIOCGSTRATEGY:
1444 {
1445 struct disk_strategy *dks = (void *)addr;
1446
1447 s = splbio();
1448 strlcpy(dks->dks_name, bufq_getstrategyname(wd->sc_q),
1449 sizeof(dks->dks_name));
1450 splx(s);
1451 dks->dks_paramlen = 0;
1452
1453 return 0;
1454 }
1455
1456 case DIOCSSTRATEGY:
1457 {
1458 struct disk_strategy *dks = (void *)addr;
1459 struct bufq_state *new;
1460 struct bufq_state *old;
1461
1462 if ((flag & FWRITE) == 0) {
1463 return EBADF;
1464 }
1465 if (dks->dks_param != NULL) {
1466 return EINVAL;
1467 }
1468 dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
1469 error = bufq_alloc(&new, dks->dks_name,
1470 BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
1471 if (error) {
1472 return error;
1473 }
1474 s = splbio();
1475 old = wd->sc_q;
1476 bufq_move(new, old);
1477 wd->sc_q = new;
1478 splx(s);
1479 bufq_free(old);
1480
1481 return 0;
1482 }
1483
1484 default:
1485 return ENOTTY;
1486 }
1487
1488 #ifdef DIAGNOSTIC
1489 panic("wdioctl: impossible");
1490 #endif
1491 }
1492
1493 #ifdef B_FORMAT
1494 int
1495 wdformat(struct buf *bp)
1496 {
1497
1498 bp->b_flags |= B_FORMAT;
1499 return wdstrategy(bp);
1500 }
1501 #endif
1502
1503 int
1504 wdsize(dev_t dev)
1505 {
1506 struct wd_softc *wd;
1507 int part, omask;
1508 int size;
1509
1510 ATADEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1511
1512 wd = device_lookup(&wd_cd, WDUNIT(dev));
1513 if (wd == NULL)
1514 return (-1);
1515
1516 part = WDPART(dev);
1517 omask = wd->sc_dk.dk_openmask & (1 << part);
1518
1519 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1520 return (-1);
1521 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1522 size = -1;
1523 else
1524 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1525 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1526 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1527 return (-1);
1528 return (size);
1529 }
1530
1531 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1532 static int wddoingadump = 0;
1533 static int wddumprecalibrated = 0;
1534
1535 /*
1536 * Dump core after a system crash.
1537 */
1538 int
1539 wddump(dev_t dev, daddr_t blkno, caddr_t va, size_t size)
1540 {
1541 struct wd_softc *wd; /* disk unit to do the I/O */
1542 struct disklabel *lp; /* disk's disklabel */
1543 int part, err;
1544 int nblks; /* total number of sectors left to write */
1545
1546 /* Check if recursive dump; if so, punt. */
1547 if (wddoingadump)
1548 return EFAULT;
1549 wddoingadump = 1;
1550
1551 wd = device_lookup(&wd_cd, WDUNIT(dev));
1552 if (wd == NULL)
1553 return (ENXIO);
1554
1555 part = WDPART(dev);
1556
1557 /* Convert to disk sectors. Request must be a multiple of size. */
1558 lp = wd->sc_dk.dk_label;
1559 if ((size % lp->d_secsize) != 0)
1560 return EFAULT;
1561 nblks = size / lp->d_secsize;
1562 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1563
1564 /* Check transfer bounds against partition size. */
1565 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1566 return EINVAL;
1567
1568 /* Offset block number to start of partition. */
1569 blkno += lp->d_partitions[part].p_offset;
1570
1571 /* Recalibrate, if first dump transfer. */
1572 if (wddumprecalibrated == 0) {
1573 wddumprecalibrated = 1;
1574 (*wd->atabus->ata_reset_drive)(wd->drvp,
1575 AT_POLL | AT_RST_EMERG);
1576 wd->drvp->state = RESET;
1577 }
1578
1579 wd->sc_bp = NULL;
1580 wd->sc_wdc_bio.blkno = blkno;
1581 wd->sc_wdc_bio.flags = ATA_POLL;
1582 if (wd->sc_flags & WDF_LBA48 &&
1583 (blkno > LBA48_THRESHOLD ||
1584 (wd->sc_quirks & WD_QUIRK_FORCE_LBA48) != 0))
1585 wd->sc_wdc_bio.flags |= ATA_LBA48;
1586 if (wd->sc_flags & WDF_LBA)
1587 wd->sc_wdc_bio.flags |= ATA_LBA;
1588 wd->sc_wdc_bio.bcount = nblks * lp->d_secsize;
1589 wd->sc_wdc_bio.databuf = va;
1590 #ifndef WD_DUMP_NOT_TRUSTED
1591 switch (wd->atabus->ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1592 case ATACMD_TRY_AGAIN:
1593 panic("wddump: try again");
1594 break;
1595 case ATACMD_QUEUED:
1596 panic("wddump: polled command has been queued");
1597 break;
1598 case ATACMD_COMPLETE:
1599 break;
1600 }
1601 switch(wd->sc_wdc_bio.error) {
1602 case TIMEOUT:
1603 printf("wddump: device timed out");
1604 err = EIO;
1605 break;
1606 case ERR_DF:
1607 printf("wddump: drive fault");
1608 err = EIO;
1609 break;
1610 case ERR_DMA:
1611 printf("wddump: DMA error");
1612 err = EIO;
1613 break;
1614 case ERROR:
1615 printf("wddump: ");
1616 wdperror(wd);
1617 err = EIO;
1618 break;
1619 case NOERROR:
1620 err = 0;
1621 break;
1622 default:
1623 panic("wddump: unknown error type");
1624 }
1625 if (err != 0) {
1626 printf("\n");
1627 return err;
1628 }
1629 #else /* WD_DUMP_NOT_TRUSTED */
1630 /* Let's just talk about this first... */
1631 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1632 unit, va, cylin, head, sector);
1633 delay(500 * 1000); /* half a second */
1634 #endif
1635
1636 wddoingadump = 0;
1637 return 0;
1638 }
1639
1640 #ifdef HAS_BAD144_HANDLING
1641 /*
1642 * Internalize the bad sector table.
1643 */
1644 void
1645 bad144intern(struct wd_softc *wd)
1646 {
1647 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1648 struct disklabel *lp = wd->sc_dk.dk_label;
1649 int i = 0;
1650
1651 ATADEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1652
1653 for (; i < NBT_BAD; i++) {
1654 if (bt->bt_bad[i].bt_cyl == 0xffff)
1655 break;
1656 wd->sc_badsect[i] =
1657 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1658 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1659 (bt->bt_bad[i].bt_trksec & 0xff);
1660 }
1661 for (; i < NBT_BAD+1; i++)
1662 wd->sc_badsect[i] = -1;
1663 }
1664 #endif
1665
1666 int
1667 wd_get_params(struct wd_softc *wd, u_int8_t flags, struct ataparams *params)
1668 {
1669 switch (wd->atabus->ata_get_params(wd->drvp, flags, params)) {
1670 case CMD_AGAIN:
1671 return 1;
1672 case CMD_ERR:
1673 /*
1674 * We `know' there's a drive here; just assume it's old.
1675 * This geometry is only used to read the MBR and print a
1676 * (false) attach message.
1677 */
1678 strncpy(params->atap_model, "ST506",
1679 sizeof params->atap_model);
1680 params->atap_config = ATA_CFG_FIXED;
1681 params->atap_cylinders = 1024;
1682 params->atap_heads = 8;
1683 params->atap_sectors = 17;
1684 params->atap_multi = 1;
1685 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1686 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1687 return 0;
1688 case CMD_OK:
1689 return 0;
1690 default:
1691 panic("wd_get_params: bad return code from ata_get_params");
1692 /* NOTREACHED */
1693 }
1694 }
1695
1696 int
1697 wd_getcache(struct wd_softc *wd, int *bitsp)
1698 {
1699 struct ataparams params;
1700
1701 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1702 return EIO;
1703 if (params.atap_cmd_set1 == 0x0000 ||
1704 params.atap_cmd_set1 == 0xffff ||
1705 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0) {
1706 *bitsp = 0;
1707 return 0;
1708 }
1709 *bitsp = DKCACHE_WCHANGE | DKCACHE_READ;
1710 if (params.atap_cmd1_en & WDC_CMD1_CACHE)
1711 *bitsp |= DKCACHE_WRITE;
1712
1713 return 0;
1714 }
1715
1716 const char at_errbits[] = "\20\10ERROR\11TIMEOU\12DF";
1717
1718 int
1719 wd_setcache(struct wd_softc *wd, int bits)
1720 {
1721 struct ataparams params;
1722 struct ata_command ata_c;
1723
1724 if (wd_get_params(wd, AT_WAIT, ¶ms) != 0)
1725 return EIO;
1726
1727 if (params.atap_cmd_set1 == 0x0000 ||
1728 params.atap_cmd_set1 == 0xffff ||
1729 (params.atap_cmd_set1 & WDC_CMD1_CACHE) == 0)
1730 return EOPNOTSUPP;
1731
1732 if ((bits & DKCACHE_READ) == 0 ||
1733 (bits & DKCACHE_SAVE) != 0)
1734 return EOPNOTSUPP;
1735
1736 memset(&ata_c, 0, sizeof(struct ata_command));
1737 ata_c.r_command = SET_FEATURES;
1738 ata_c.r_st_bmask = 0;
1739 ata_c.r_st_pmask = 0;
1740 ata_c.timeout = 30000; /* 30s timeout */
1741 ata_c.flags = AT_WAIT;
1742 if (bits & DKCACHE_WRITE)
1743 ata_c.r_features = WDSF_WRITE_CACHE_EN;
1744 else
1745 ata_c.r_features = WDSF_WRITE_CACHE_DS;
1746 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1747 printf("%s: wd_setcache command not complete\n",
1748 wd->sc_dev.dv_xname);
1749 return EIO;
1750 }
1751 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1752 char sbuf[sizeof(at_errbits) + 64];
1753 bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
1754 printf("%s: wd_setcache: status=%s\n", wd->sc_dev.dv_xname,
1755 sbuf);
1756 return EIO;
1757 }
1758 return 0;
1759 }
1760
1761 int
1762 wd_standby(struct wd_softc *wd, int flags)
1763 {
1764 struct ata_command ata_c;
1765
1766 memset(&ata_c, 0, sizeof(struct ata_command));
1767 ata_c.r_command = WDCC_STANDBY_IMMED;
1768 ata_c.r_st_bmask = WDCS_DRDY;
1769 ata_c.r_st_pmask = WDCS_DRDY;
1770 ata_c.flags = flags;
1771 ata_c.timeout = 30000; /* 30s timeout */
1772 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1773 printf("%s: standby immediate command didn't complete\n",
1774 wd->sc_dev.dv_xname);
1775 return EIO;
1776 }
1777 if (ata_c.flags & AT_ERROR) {
1778 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1779 return ENODEV;
1780 }
1781 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1782 char sbuf[sizeof(at_errbits) + 64];
1783 bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
1784 printf("%s: wd_standby: status=%s\n", wd->sc_dev.dv_xname,
1785 sbuf);
1786 return EIO;
1787 }
1788 return 0;
1789 }
1790
1791 int
1792 wd_flushcache(struct wd_softc *wd, int flags)
1793 {
1794 struct ata_command ata_c;
1795
1796 /*
1797 * WDCC_FLUSHCACHE is here since ATA-4, but some drives report
1798 * only ATA-2 and still support it.
1799 */
1800 if (wd->drvp->ata_vers < 4 &&
1801 ((wd->sc_params.atap_cmd_set2 & WDC_CMD2_FC) == 0 ||
1802 wd->sc_params.atap_cmd_set2 == 0xffff))
1803 return ENODEV;
1804 memset(&ata_c, 0, sizeof(struct ata_command));
1805 if ((wd->sc_params.atap_cmd2_en & ATA_CMD2_LBA48) != 0 &&
1806 (wd->sc_params.atap_cmd2_en & ATA_CMD2_FCE) != 0)
1807 ata_c.r_command = WDCC_FLUSHCACHE_EXT;
1808 else
1809 ata_c.r_command = WDCC_FLUSHCACHE;
1810 ata_c.r_st_bmask = WDCS_DRDY;
1811 ata_c.r_st_pmask = WDCS_DRDY;
1812 ata_c.flags = flags;
1813 ata_c.timeout = 30000; /* 30s timeout */
1814 if (wd->atabus->ata_exec_command(wd->drvp, &ata_c) != ATACMD_COMPLETE) {
1815 printf("%s: flush cache command didn't complete\n",
1816 wd->sc_dev.dv_xname);
1817 return EIO;
1818 }
1819 if (ata_c.flags & AT_ERROR) {
1820 if (ata_c.r_error == WDCE_ABRT) /* command not supported */
1821 return ENODEV;
1822 }
1823 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1824 char sbuf[sizeof(at_errbits) + 64];
1825 bitmask_snprintf(ata_c.flags, at_errbits, sbuf, sizeof(sbuf));
1826 printf("%s: wd_flushcache: status=%s\n", wd->sc_dev.dv_xname,
1827 sbuf);
1828 return EIO;
1829 }
1830 return 0;
1831 }
1832
1833 void
1834 wd_shutdown(void *arg)
1835 {
1836 struct wd_softc *wd = arg;
1837 wd_flushcache(wd, AT_POLL);
1838 }
1839
1840 /*
1841 * Allocate space for a ioctl queue structure. Mostly taken from
1842 * scsipi_ioctl.c
1843 */
1844 struct wd_ioctl *
1845 wi_get(void)
1846 {
1847 struct wd_ioctl *wi;
1848 int s;
1849
1850 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK|M_ZERO);
1851 simple_lock_init(&wi->wi_bp.b_interlock);
1852 s = splbio();
1853 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1854 splx(s);
1855 return (wi);
1856 }
1857
1858 /*
1859 * Free an ioctl structure and remove it from our list
1860 */
1861
1862 void
1863 wi_free(struct wd_ioctl *wi)
1864 {
1865 int s;
1866
1867 s = splbio();
1868 LIST_REMOVE(wi, wi_list);
1869 splx(s);
1870 free(wi, M_TEMP);
1871 }
1872
1873 /*
1874 * Find a wd_ioctl structure based on the struct buf.
1875 */
1876
1877 struct wd_ioctl *
1878 wi_find(struct buf *bp)
1879 {
1880 struct wd_ioctl *wi;
1881 int s;
1882
1883 s = splbio();
1884 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1885 if (bp == &wi->wi_bp)
1886 break;
1887 splx(s);
1888 return (wi);
1889 }
1890
1891 /*
1892 * Ioctl pseudo strategy routine
1893 *
1894 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1895 * happens here is:
1896 *
1897 * - wdioctl() queues a wd_ioctl structure.
1898 *
1899 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1900 * user space I/O is required. If physio() is called, physio() eventually
1901 * calls wdioctlstrategy().
1902 *
1903 * - In either case, wdioctlstrategy() calls wd->atabus->ata_exec_command()
1904 * to perform the actual command
1905 *
1906 * The reason for the use of the pseudo strategy routine is because
1907 * when doing I/O to/from user space, physio _really_ wants to be in
1908 * the loop. We could put the entire buffer into the ioctl request
1909 * structure, but that won't scale if we want to do things like download
1910 * microcode.
1911 */
1912
1913 void
1914 wdioctlstrategy(struct buf *bp)
1915 {
1916 struct wd_ioctl *wi;
1917 struct ata_command ata_c;
1918 int error = 0;
1919
1920 wi = wi_find(bp);
1921 if (wi == NULL) {
1922 printf("wdioctlstrategy: "
1923 "No matching ioctl request found in queue\n");
1924 error = EINVAL;
1925 goto bad;
1926 }
1927
1928 memset(&ata_c, 0, sizeof(ata_c));
1929
1930 /*
1931 * Abort if physio broke up the transfer
1932 */
1933
1934 if (bp->b_bcount != wi->wi_atareq.datalen) {
1935 printf("physio split wd ioctl request... cannot proceed\n");
1936 error = EIO;
1937 goto bad;
1938 }
1939
1940 /*
1941 * Abort if we didn't get a buffer size that was a multiple of
1942 * our sector size (or was larger than NBBY)
1943 */
1944
1945 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1946 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1947 (1 << NBBY)) {
1948 error = EINVAL;
1949 goto bad;
1950 }
1951
1952 /*
1953 * Make sure a timeout was supplied in the ioctl request
1954 */
1955
1956 if (wi->wi_atareq.timeout == 0) {
1957 error = EINVAL;
1958 goto bad;
1959 }
1960
1961 if (wi->wi_atareq.flags & ATACMD_READ)
1962 ata_c.flags |= AT_READ;
1963 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1964 ata_c.flags |= AT_WRITE;
1965
1966 if (wi->wi_atareq.flags & ATACMD_READREG)
1967 ata_c.flags |= AT_READREG;
1968
1969 ata_c.flags |= AT_WAIT;
1970
1971 ata_c.timeout = wi->wi_atareq.timeout;
1972 ata_c.r_command = wi->wi_atareq.command;
1973 ata_c.r_head = wi->wi_atareq.head & 0x0f;
1974 ata_c.r_cyl = wi->wi_atareq.cylinder;
1975 ata_c.r_sector = wi->wi_atareq.sec_num;
1976 ata_c.r_count = wi->wi_atareq.sec_count;
1977 ata_c.r_features = wi->wi_atareq.features;
1978 ata_c.r_st_bmask = WDCS_DRDY;
1979 ata_c.r_st_pmask = WDCS_DRDY;
1980 ata_c.data = wi->wi_bp.b_data;
1981 ata_c.bcount = wi->wi_bp.b_bcount;
1982
1983 if (wi->wi_softc->atabus->ata_exec_command(wi->wi_softc->drvp, &ata_c)
1984 != ATACMD_COMPLETE) {
1985 wi->wi_atareq.retsts = ATACMD_ERROR;
1986 goto bad;
1987 }
1988
1989 if (ata_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1990 if (ata_c.flags & AT_ERROR) {
1991 wi->wi_atareq.retsts = ATACMD_ERROR;
1992 wi->wi_atareq.error = ata_c.r_error;
1993 } else if (ata_c.flags & AT_DF)
1994 wi->wi_atareq.retsts = ATACMD_DF;
1995 else
1996 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1997 } else {
1998 wi->wi_atareq.retsts = ATACMD_OK;
1999 if (wi->wi_atareq.flags & ATACMD_READREG) {
2000 wi->wi_atareq.head = ata_c.r_head ;
2001 wi->wi_atareq.cylinder = ata_c.r_cyl;
2002 wi->wi_atareq.sec_num = ata_c.r_sector;
2003 wi->wi_atareq.sec_count = ata_c.r_count;
2004 wi->wi_atareq.features = ata_c.r_features;
2005 wi->wi_atareq.error = ata_c.r_error;
2006 }
2007 }
2008
2009 bp->b_error = 0;
2010 biodone(bp);
2011 return;
2012 bad:
2013 bp->b_flags |= B_ERROR;
2014 bp->b_error = error;
2015 biodone(bp);
2016 }
2017