wd.c revision 1.187 1 /* $NetBSD: wd.c,v 1.187 1998/11/20 01:23:52 thorpej Exp $ */
2
3 /*
4 * Copyright (c) 1998 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 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 #define WDCDEBUG
69
70 #include "rnd.h"
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/conf.h>
76 #include <sys/file.h>
77 #include <sys/stat.h>
78 #include <sys/ioctl.h>
79 #include <sys/buf.h>
80 #include <sys/uio.h>
81 #include <sys/malloc.h>
82 #include <sys/device.h>
83 #include <sys/disklabel.h>
84 #include <sys/disk.h>
85 #include <sys/syslog.h>
86 #include <sys/proc.h>
87 #if NRND > 0
88 #include <sys/rnd.h>
89 #endif
90
91 #include <vm/vm.h>
92
93 #include <machine/intr.h>
94 #include <machine/bus.h>
95
96 #include <dev/ata/atareg.h>
97 #include <dev/ata/atavar.h>
98 #include <dev/ata/wdvar.h>
99 #include <dev/ic/wdcreg.h>
100 #include <sys/ataio.h>
101 #include "locators.h"
102
103 #define WAITTIME (4 * hz) /* time to wait for a completion */
104 #define WDIORETRIES 5 /* number of retries before giving up */
105 #define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
106
107 #define WDUNIT(dev) DISKUNIT(dev)
108 #define WDPART(dev) DISKPART(dev)
109 #define MAKEWDDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
110
111 #define WDLABELDEV(dev) (MAKEWDDEV(major(dev), WDUNIT(dev), RAW_PART))
112
113 #define DEBUG_INTR 0x01
114 #define DEBUG_XFERS 0x02
115 #define DEBUG_STATUS 0x04
116 #define DEBUG_FUNCS 0x08
117 #define DEBUG_PROBE 0x10
118 #ifdef WDCDEBUG
119 extern int wdcdebug_wd_mask; /* init'ed in ata_wdc.c */
120 #define WDCDEBUG_PRINT(args, level) \
121 if (wdcdebug_wd_mask & (level)) \
122 printf args
123 #else
124 #define WDCDEBUG_PRINT(args, level)
125 #endif
126
127 struct wd_softc {
128 /* General disk infos */
129 struct device sc_dev;
130 struct disk sc_dk;
131 struct buf sc_q;
132 /* IDE disk soft states */
133 struct ata_bio sc_wdc_bio; /* current transfer */
134 struct buf *sc_bp; /* buf being transfered */
135 void *wdc_softc; /* pointer to our parent */
136 struct ata_drive_datas *drvp; /* Our controller's infos */
137 int openings;
138 struct ataparams sc_params;/* drive characteistics found */
139 int sc_flags;
140 #define WDF_LOCKED 0x01
141 #define WDF_WANTED 0x02
142 #define WDF_WLABEL 0x04 /* label is writable */
143 #define WDF_LABELLING 0x08 /* writing label */
144 /*
145 * XXX Nothing resets this yet, but disk change sensing will when ATA-4 is
146 * more fully implemented.
147 */
148 #define WDF_LOADED 0x10 /* parameters loaded */
149 #define WDF_WAIT 0x20 /* waiting for resources */
150 #define WDF_LBA 0x40 /* using LBA mode */
151 int sc_capacity;
152 int cyl; /* actual drive parameters */
153 int heads;
154 int sectors;
155 int retries; /* number of xfer retry */
156 #if NRND > 0
157 rndsource_element_t rnd_source;
158 #endif
159 };
160
161 #define sc_drive sc_wdc_bio.drive
162 #define sc_mode sc_wdc_bio.mode
163 #define sc_multi sc_wdc_bio.multi
164 #define sc_badsect sc_wdc_bio.badsect
165
166 int wdprobe __P((struct device *, struct cfdata *, void *));
167 void wdattach __P((struct device *, struct device *, void *));
168 int wdprint __P((void *, char *));
169
170 struct cfattach wd_ca = {
171 sizeof(struct wd_softc), wdprobe, wdattach
172 };
173
174 extern struct cfdriver wd_cd;
175
176 /*
177 * Glue necessary to hook WDCIOCCOMMAND into physio
178 */
179
180 struct wd_ioctl {
181 LIST_ENTRY(wd_ioctl) wi_list;
182 struct buf wi_bp;
183 struct uio wi_uio;
184 struct iovec wi_iov;
185 atareq_t wi_atareq;
186 struct wd_softc *wi_softc;
187 };
188
189 LIST_HEAD(, wd_ioctl) wi_head;
190
191 struct wd_ioctl *wi_find __P((struct buf *));
192 void wi_free __P((struct wd_ioctl *));
193 struct wd_ioctl *wi_get __P((void));
194 void wdioctlstrategy __P((struct buf *));
195
196 void wdgetdefaultlabel __P((struct wd_softc *, struct disklabel *));
197 void wdgetdisklabel __P((struct wd_softc *));
198 void wdstrategy __P((struct buf *));
199 void wdstart __P((void *));
200 void __wdstart __P((struct wd_softc*, struct buf *));
201 void wdrestart __P((void*));
202 int wd_get_params __P((struct wd_softc *, u_int8_t, struct ataparams *));
203
204 struct dkdriver wddkdriver = { wdstrategy };
205
206 /* XXX: these should go elsewhere */
207 cdev_decl(wd);
208 bdev_decl(wd);
209
210 #ifdef HAS_BAD144_HANDLING
211 static void bad144intern __P((struct wd_softc *));
212 #endif
213 int wdlock __P((struct wd_softc *));
214 void wdunlock __P((struct wd_softc *));
215 void print_wderror __P((int, char*));
216
217 int
218 wdprobe(parent, match, aux)
219 struct device *parent;
220 struct cfdata *match;
221
222 void *aux;
223 {
224 struct ata_atapi_attach *aa_link = aux;
225
226 if (aa_link == NULL)
227 return 0;
228 if (aa_link->aa_type != T_ATA)
229 return 0;
230
231 if (match->cf_loc[ATACF_CHANNEL] != ATACF_CHANNEL_DEFAULT &&
232 match->cf_loc[ATACF_CHANNEL] != aa_link->aa_channel)
233 return 0;
234
235 if (match->cf_loc[ATACF_DRIVE] != ATACF_DRIVE_DEFAULT &&
236 match->cf_loc[ATACF_DRIVE] != aa_link->aa_drv_data->drive)
237 return 0;
238 return 1;
239 }
240
241 void
242 wdattach(parent, self, aux)
243 struct device *parent, *self;
244 void *aux;
245 {
246 struct wd_softc *wd = (void *)self;
247 struct ata_atapi_attach *aa_link= aux;
248 int i, blank;
249 char buf[41], c, *p, *q;
250 WDCDEBUG_PRINT(("wdattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
251
252 wd->openings = aa_link->aa_openings;
253 wd->drvp = aa_link->aa_drv_data;;
254 wd->wdc_softc = parent;
255 /* give back our softc to our caller */
256 wd->drvp->drv_softc = &wd->sc_dev;
257
258 /* read our drive info */
259 if (wd_get_params(wd, AT_POLL, &wd->sc_params) != 0) {
260 printf("%s: IDENTIFY failed\n", wd->sc_dev.dv_xname);
261 return;
262 }
263
264 for (blank = 0, p = wd->sc_params.atap_model, q = buf, i = 0;
265 i < sizeof(wd->sc_params.atap_model); i++) {
266 c = *p++;
267 if (c == '\0')
268 break;
269 if (c != ' ') {
270 if (blank) {
271 *q++ = ' ';
272 blank = 0;
273 }
274 *q++ = c;
275 } else
276 blank = 1;
277 }
278 *q++ = '\0';
279
280 printf(": <%s>\n", buf);
281
282 if ((wd->sc_params.atap_multi & 0xff) > 1) {
283 wd->sc_multi = wd->sc_params.atap_multi & 0xff;
284 } else {
285 wd->sc_multi = 1;
286 }
287
288 printf("%s: drive supports %d-sector pio transfers,",
289 wd->sc_dev.dv_xname, wd->sc_multi);
290
291 /* Prior to ATA-4, LBA was optional. */
292 if ((wd->sc_params.atap_capabilities1 & WDC_CAP_LBA) != 0)
293 wd->sc_flags |= WDF_LBA;
294 #if 0
295 /* ATA-4 requires LBA. */
296 if (wd->sc_params.atap_ataversion != 0xffff &&
297 wd->sc_params.atap_ataversion >= WDC_VER_ATA4)
298 wd->sc_flags |= WDF_LBA;
299 #endif
300
301 if ((wd->sc_flags & WDF_LBA) != 0) {
302 printf(" lba addressing\n");
303 wd->sc_capacity =
304 (wd->sc_params.atap_capacity[1] << 16) |
305 wd->sc_params.atap_capacity[0];
306 printf("%s: %dMB, %d cyl, %d head, %d sec, "
307 "%d bytes/sect x %d sectors\n",
308 self->dv_xname,
309 wd->sc_capacity / (1048576 / DEV_BSIZE),
310 wd->sc_params.atap_cylinders,
311 wd->sc_params.atap_heads,
312 wd->sc_params.atap_sectors,
313 DEV_BSIZE,
314 wd->sc_capacity);
315 } else {
316 printf(" chs addressing\n");
317 wd->sc_capacity =
318 wd->sc_params.atap_cylinders *
319 wd->sc_params.atap_heads *
320 wd->sc_params.atap_sectors;
321 printf("%s %dMB, %d cyl, %d head, %d sec, %d bytes/sect x %d "
322 "sectors\n", self->dv_xname,
323 wd->sc_capacity / (1048576 / DEV_BSIZE),
324 wd->sc_params.atap_cylinders,
325 wd->sc_params.atap_heads,
326 wd->sc_params.atap_sectors,
327 DEV_BSIZE,
328 wd->sc_capacity);
329 }
330 WDCDEBUG_PRINT(("%s: atap_dmatiming_mimi=%d, atap_dmatiming_recom=%d\n",
331 self->dv_xname, wd->sc_params.atap_dmatiming_mimi,
332 wd->sc_params.atap_dmatiming_recom), DEBUG_PROBE);
333 /*
334 * Initialize and attach the disk structure.
335 */
336 wd->sc_dk.dk_driver = &wddkdriver;
337 wd->sc_dk.dk_name = wd->sc_dev.dv_xname;
338 disk_attach(&wd->sc_dk);
339 wd->sc_wdc_bio.lp = wd->sc_dk.dk_label;
340
341 #if NRND > 0
342 rnd_attach_source(&wd->rnd_source, wd->sc_dev.dv_xname, RND_TYPE_DISK);
343 #endif
344 }
345
346 /*
347 * Read/write routine for a buffer. Validates the arguments and schedules the
348 * transfer. Does not wait for the transfer to complete.
349 */
350 void
351 wdstrategy(bp)
352 struct buf *bp;
353 {
354 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(bp->b_dev)];
355 int s;
356 WDCDEBUG_PRINT(("wdstrategy (%s)\n", wd->sc_dev.dv_xname),
357 DEBUG_XFERS);
358
359 /* Valid request? */
360 if (bp->b_blkno < 0 ||
361 (bp->b_bcount % wd->sc_dk.dk_label->d_secsize) != 0 ||
362 (bp->b_bcount / wd->sc_dk.dk_label->d_secsize) >= (1 << NBBY)) {
363 bp->b_error = EINVAL;
364 goto bad;
365 }
366
367 /* If device invalidated (e.g. media change, door open), error. */
368 if ((wd->sc_flags & WDF_LOADED) == 0) {
369 bp->b_error = EIO;
370 goto bad;
371 }
372
373 /* If it's a null transfer, return immediately. */
374 if (bp->b_bcount == 0)
375 goto done;
376
377 /*
378 * Do bounds checking, adjust transfer. if error, process.
379 * If end of partition, just return.
380 */
381 if (WDPART(bp->b_dev) != RAW_PART &&
382 bounds_check_with_label(bp, wd->sc_dk.dk_label,
383 (wd->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
384 goto done;
385 /* Queue transfer on drive, activate drive and controller if idle. */
386 s = splbio();
387 disksort(&wd->sc_q, bp);
388 wdstart(wd);
389 splx(s);
390 return;
391 bad:
392 bp->b_flags |= B_ERROR;
393 done:
394 /* Toss transfer; we're done early. */
395 bp->b_resid = bp->b_bcount;
396 biodone(bp);
397 }
398
399 /*
400 * Queue a drive for I/O.
401 */
402 void
403 wdstart(arg)
404 void *arg;
405 {
406 struct wd_softc *wd = arg;
407 struct buf *dp, *bp=0;
408
409 WDCDEBUG_PRINT(("wdstart %s\n", wd->sc_dev.dv_xname),
410 DEBUG_XFERS);
411 while (wd->openings > 0) {
412
413 /* Is there a buf for us ? */
414 dp = &wd->sc_q;
415 if ((bp = dp->b_actf) == NULL) /* yes, an assign */
416 return;
417 dp->b_actf = bp->b_actf;
418
419 /*
420 * Make the command. First lock the device
421 */
422 wd->openings--;
423
424 wd->retries = 0;
425 __wdstart(wd, bp);
426 }
427 }
428
429 void
430 __wdstart(wd, bp)
431 struct wd_softc *wd;
432 struct buf *bp;
433 {
434 daddr_t p_offset;
435 if (WDPART(bp->b_dev) != RAW_PART)
436 p_offset =
437 wd->sc_dk.dk_label->d_partitions[WDPART(bp->b_dev)].p_offset;
438 else
439 p_offset = 0;
440 wd->sc_wdc_bio.blkno = bp->b_blkno + p_offset;
441 wd->sc_wdc_bio.blkno /= (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
442 wd->sc_wdc_bio.blkdone =0;
443 wd->sc_bp = bp;
444 /*
445 * If we're retrying, retry in single-sector mode. This will give us
446 * the sector number of the problem, and will eventually allow the
447 * transfer to succeed.
448 */
449 if (wd->sc_multi == 1 || wd->retries > 0)
450 wd->sc_wdc_bio.flags = ATA_SINGLE;
451 else
452 wd->sc_wdc_bio.flags = 0;
453 if (wd->sc_flags & WDF_LBA)
454 wd->sc_wdc_bio.flags |= ATA_LBA;
455 if (bp->b_flags & B_READ)
456 wd->sc_wdc_bio.flags |= ATA_READ;
457 wd->sc_wdc_bio.bcount = bp->b_bcount;
458 wd->sc_wdc_bio.databuf = bp->b_data;
459 /* Instrumentation. */
460 disk_busy(&wd->sc_dk);
461 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
462 case WDC_TRY_AGAIN:
463 timeout(wdrestart, wd, hz);
464 break;
465 case WDC_QUEUED:
466 break;
467 case WDC_COMPLETE:
468 wddone(wd);
469 break;
470 default:
471 panic("__wdstart: bad return code from wdc_ata_bio()");
472 }
473 }
474
475 void
476 wddone(v)
477 void *v;
478 {
479 struct wd_softc *wd = v;
480 struct buf *bp = wd->sc_bp;
481 char buf[256], *errbuf = buf;
482 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
483 DEBUG_XFERS);
484
485 bp->b_resid = wd->sc_wdc_bio.bcount;
486 errbuf[0] = '\0';
487 switch (wd->sc_wdc_bio.error) {
488 case ERR_DMA:
489 errbuf = "DMA error";
490 goto retry;
491 case ERR_DF:
492 errbuf = "device fault";
493 goto retry;
494 case TIMEOUT:
495 errbuf = "device timeout";
496 goto retry;
497 case ERROR:
498 /* Don't care about media change bits */
499 if (wd->sc_wdc_bio.r_error != 0 &&
500 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
501 goto noerror;
502 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
503 retry: /* Just reset and retry. Can we do more ? */
504 wdc_reset_channel(wd->drvp);
505 diskerr(bp, "wd", errbuf, LOG_PRINTF,
506 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
507 if (wd->retries++ < WDIORETRIES) {
508 printf(", retrying\n");
509 timeout(wdrestart, wd, RECOVERYTIME);
510 return;
511 }
512 printf("\n");
513 bp->b_flags |= B_ERROR;
514 bp->b_error = EIO;
515 break;
516 case NOERROR:
517 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
518 printf("%s: soft error (corrected)\n",
519 wd->sc_dev.dv_xname);
520 }
521 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
522 #if NRND > 0
523 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
524 #endif
525 biodone(bp);
526 wd->openings++;
527 wdstart(wd);
528 }
529
530 void
531 wdrestart(v)
532 void *v;
533 {
534 struct wd_softc *wd = v;
535 struct buf *bp = wd->sc_bp;
536 int s;
537 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
538 DEBUG_XFERS);
539
540 s = splbio();
541 __wdstart(v, bp);
542 splx(s);
543 }
544
545 int
546 wdread(dev, uio, flags)
547 dev_t dev;
548 struct uio *uio;
549 int flags;
550 {
551
552 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
553 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
554 }
555
556 int
557 wdwrite(dev, uio, flags)
558 dev_t dev;
559 struct uio *uio;
560 int flags;
561 {
562
563 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
564 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
565 }
566
567 /*
568 * Wait interruptibly for an exclusive lock.
569 *
570 * XXX
571 * Several drivers do this; it should be abstracted and made MP-safe.
572 */
573 int
574 wdlock(wd)
575 struct wd_softc *wd;
576 {
577 int error;
578 int s;
579
580 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
581
582 s = splbio();
583
584 while ((wd->sc_flags & WDF_LOCKED) != 0) {
585 wd->sc_flags |= WDF_WANTED;
586 if ((error = tsleep(wd, PRIBIO | PCATCH,
587 "wdlck", 0)) != 0) {
588 splx(s);
589 return error;
590 }
591 }
592 wd->sc_flags |= WDF_LOCKED;
593 splx(s);
594 return 0;
595 }
596
597 /*
598 * Unlock and wake up any waiters.
599 */
600 void
601 wdunlock(wd)
602 struct wd_softc *wd;
603 {
604
605 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
606
607 wd->sc_flags &= ~WDF_LOCKED;
608 if ((wd->sc_flags & WDF_WANTED) != 0) {
609 wd->sc_flags &= ~WDF_WANTED;
610 wakeup(wd);
611 }
612 }
613
614 int
615 wdopen(dev, flag, fmt, p)
616 dev_t dev;
617 int flag, fmt;
618 struct proc *p;
619 {
620 struct wd_softc *wd;
621 int unit, part;
622 int error;
623
624 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
625 unit = WDUNIT(dev);
626 if (unit >= wd_cd.cd_ndevs)
627 return ENXIO;
628 wd = wd_cd.cd_devs[unit];
629 if (wd == NULL)
630 return ENXIO;
631
632 /*
633 * If this is the first open of this device, add a reference
634 * to the adapter.
635 */
636 if (wd->sc_dk.dk_openmask == 0 &&
637 (error = wdc_ata_addref(wd->drvp)) != 0)
638 return (error);
639
640 if ((error = wdlock(wd)) != 0)
641 goto bad4;
642
643 if (wd->sc_dk.dk_openmask != 0) {
644 /*
645 * If any partition is open, but the disk has been invalidated,
646 * disallow further opens.
647 */
648 if ((wd->sc_flags & WDF_LOADED) == 0) {
649 error = EIO;
650 goto bad3;
651 }
652 } else {
653 if ((wd->sc_flags & WDF_LOADED) == 0) {
654 wd->sc_flags |= WDF_LOADED;
655
656 /* Load the physical device parameters. */
657 wd_get_params(wd, AT_WAIT, &wd->sc_params);
658
659 /* Load the partition info if not already loaded. */
660 wdgetdisklabel(wd);
661 }
662 }
663
664 part = WDPART(dev);
665
666 /* Check that the partition exists. */
667 if (part != RAW_PART &&
668 (part >= wd->sc_dk.dk_label->d_npartitions ||
669 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
670 error = ENXIO;
671 goto bad;
672 }
673
674 /* Insure only one open at a time. */
675 switch (fmt) {
676 case S_IFCHR:
677 wd->sc_dk.dk_copenmask |= (1 << part);
678 break;
679 case S_IFBLK:
680 wd->sc_dk.dk_bopenmask |= (1 << part);
681 break;
682 }
683 wd->sc_dk.dk_openmask =
684 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
685
686 wdunlock(wd);
687 return 0;
688
689 bad:
690 if (wd->sc_dk.dk_openmask == 0) {
691 }
692
693 bad3:
694 wdunlock(wd);
695 bad4:
696 if (wd->sc_dk.dk_openmask == 0)
697 wdc_ata_delref(wd->drvp);
698 return error;
699 }
700
701 int
702 wdclose(dev, flag, fmt, p)
703 dev_t dev;
704 int flag, fmt;
705 struct proc *p;
706 {
707 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
708 int part = WDPART(dev);
709 int error;
710
711 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
712 if ((error = wdlock(wd)) != 0)
713 return error;
714
715 switch (fmt) {
716 case S_IFCHR:
717 wd->sc_dk.dk_copenmask &= ~(1 << part);
718 break;
719 case S_IFBLK:
720 wd->sc_dk.dk_bopenmask &= ~(1 << part);
721 break;
722 }
723 wd->sc_dk.dk_openmask =
724 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
725
726 if (wd->sc_dk.dk_openmask == 0) {
727 /* XXXX Must wait for I/O to complete! */
728
729 wdc_ata_delref(wd->drvp);
730 }
731
732 wdunlock(wd);
733 return 0;
734 }
735
736 void
737 wdgetdefaultlabel(wd, lp)
738 struct wd_softc *wd;
739 struct disklabel *lp;
740 {
741
742 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
743 memset(lp, 0, sizeof(struct disklabel));
744
745 lp->d_secsize = DEV_BSIZE;
746 lp->d_ntracks = wd->sc_params.atap_heads;
747 lp->d_nsectors = wd->sc_params.atap_sectors;
748 lp->d_ncylinders = wd->sc_params.atap_cylinders;
749 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
750
751 #if 0
752 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
753 lp->d_type = DTYPE_ST506;
754 strncpy(lp->d_typename, "ST506 disk", 16);
755 } else {
756 lp->d_type = DTYPE_ESDI;
757 strncpy(lp->d_typename, "ESDI/IDE",
758 sizeof lp->d_typename);
759 }
760 #endif
761 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
762 strncpy(lp->d_packname, "fictitious", 16);
763 lp->d_secperunit = wd->sc_capacity;
764 lp->d_rpm = 3600;
765 lp->d_interleave = 1;
766 lp->d_flags = 0;
767
768 lp->d_partitions[RAW_PART].p_offset = 0;
769 lp->d_partitions[RAW_PART].p_size =
770 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
771 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
772 lp->d_npartitions = RAW_PART + 1;
773
774 lp->d_magic = DISKMAGIC;
775 lp->d_magic2 = DISKMAGIC;
776 lp->d_checksum = dkcksum(lp);
777 }
778
779 /*
780 * Fabricate a default disk label, and try to read the correct one.
781 */
782 void
783 wdgetdisklabel(wd)
784 struct wd_softc *wd;
785 {
786 struct disklabel *lp = wd->sc_dk.dk_label;
787 char *errstring;
788
789 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
790
791 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
792
793 wdgetdefaultlabel(wd, lp);
794
795 wd->sc_badsect[0] = -1;
796
797 if (wd->drvp->state > RECAL)
798 wd->drvp->drive_flags |= DRIVE_RESET;
799 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
800 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
801 if (errstring) {
802 /*
803 * This probably happened because the drive's default
804 * geometry doesn't match the DOS geometry. We
805 * assume the DOS geometry is now in the label and try
806 * again. XXX This is a kluge.
807 */
808 if (wd->drvp->state > RECAL)
809 wd->drvp->drive_flags |= DRIVE_RESET;
810 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
811 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
812 }
813 if (errstring) {
814 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
815 return;
816 }
817
818 if (wd->drvp->state > RECAL)
819 wd->drvp->drive_flags |= DRIVE_RESET;
820 #ifdef HAS_BAD144_HANDLING
821 if ((lp->d_flags & D_BADSECT) != 0)
822 bad144intern(wd);
823 #endif
824 }
825
826 int
827 wdioctl(dev, xfer, addr, flag, p)
828 dev_t dev;
829 u_long xfer;
830 caddr_t addr;
831 int flag;
832 struct proc *p;
833 {
834 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
835 int error;
836
837 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
838
839 if ((wd->sc_flags & WDF_LOADED) == 0)
840 return EIO;
841
842 switch (xfer) {
843 #ifdef HAS_BAD144_HANDLING
844 case DIOCSBAD:
845 if ((flag & FWRITE) == 0)
846 return EBADF;
847 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
848 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
849 bad144intern(wd);
850 return 0;
851 #endif
852
853 case DIOCGDINFO:
854 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
855 return 0;
856
857 case DIOCGPART:
858 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
859 ((struct partinfo *)addr)->part =
860 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
861 return 0;
862
863 case DIOCWDINFO:
864 case DIOCSDINFO:
865 if ((flag & FWRITE) == 0)
866 return EBADF;
867
868 if ((error = wdlock(wd)) != 0)
869 return error;
870 wd->sc_flags |= WDF_LABELLING;
871
872 error = setdisklabel(wd->sc_dk.dk_label,
873 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
874 wd->sc_dk.dk_cpulabel);
875 if (error == 0) {
876 if (wd->drvp->state > RECAL)
877 wd->drvp->drive_flags |= DRIVE_RESET;
878 if (xfer == DIOCWDINFO)
879 error = writedisklabel(WDLABELDEV(dev),
880 wdstrategy, wd->sc_dk.dk_label,
881 wd->sc_dk.dk_cpulabel);
882 }
883
884 wd->sc_flags &= ~WDF_LABELLING;
885 wdunlock(wd);
886 return error;
887
888 case DIOCWLABEL:
889 if ((flag & FWRITE) == 0)
890 return EBADF;
891 if (*(int *)addr)
892 wd->sc_flags |= WDF_WLABEL;
893 else
894 wd->sc_flags &= ~WDF_WLABEL;
895 return 0;
896
897 case DIOCGDEFLABEL:
898 wdgetdefaultlabel(wd, (struct disklabel *)addr);
899 return 0;
900
901 #ifdef notyet
902 case DIOCWFORMAT:
903 if ((flag & FWRITE) == 0)
904 return EBADF;
905 {
906 register struct format_op *fop;
907 struct iovec aiov;
908 struct uio auio;
909
910 fop = (struct format_op *)addr;
911 aiov.iov_base = fop->df_buf;
912 aiov.iov_len = fop->df_count;
913 auio.uio_iov = &aiov;
914 auio.uio_iovcnt = 1;
915 auio.uio_resid = fop->df_count;
916 auio.uio_segflg = 0;
917 auio.uio_offset =
918 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
919 auio.uio_procp = p;
920 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
921 &auio);
922 fop->df_count -= auio.uio_resid;
923 fop->df_reg[0] = wdc->sc_status;
924 fop->df_reg[1] = wdc->sc_error;
925 return error;
926 }
927 #endif
928
929 case ATAIOCCOMMAND:
930 /*
931 * Make sure this command is (relatively) safe first
932 */
933 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
934 (flag & FWRITE) == 0)
935 return (EBADF);
936 {
937 struct wd_ioctl *wi;
938 atareq_t *atareq = (atareq_t *) addr;
939 int error;
940
941 wi = wi_get();
942 wi->wi_softc = wd;
943 wi->wi_atareq = *atareq;
944
945 if (atareq->datalen && atareq->flags &
946 (ATACMD_READ | ATACMD_WRITE)) {
947 wi->wi_iov.iov_base = atareq->databuf;
948 wi->wi_iov.iov_len = atareq->datalen;
949 wi->wi_uio.uio_iov = &wi->wi_iov;
950 wi->wi_uio.uio_iovcnt = 1;
951 wi->wi_uio.uio_resid = atareq->datalen;
952 wi->wi_uio.uio_offset = 0;
953 wi->wi_uio.uio_segflg = UIO_USERSPACE;
954 wi->wi_uio.uio_rw =
955 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
956 wi->wi_uio.uio_procp = p;
957 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
958 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
959 minphys, &wi->wi_uio);
960 } else {
961 /* No need to call physio if we don't have any
962 user data */
963 wi->wi_bp.b_flags = 0;
964 wi->wi_bp.b_data = 0;
965 wi->wi_bp.b_bcount = 0;
966 wi->wi_bp.b_dev = 0;
967 wi->wi_bp.b_proc = p;
968 wdioctlstrategy(&wi->wi_bp);
969 error = wi->wi_bp.b_error;
970 }
971 *atareq = wi->wi_atareq;
972 wi_free(wi);
973 return(error);
974 }
975
976 default:
977 return ENOTTY;
978 }
979
980 #ifdef DIAGNOSTIC
981 panic("wdioctl: impossible");
982 #endif
983 }
984
985 #ifdef B_FORMAT
986 int
987 wdformat(struct buf *bp)
988 {
989
990 bp->b_flags |= B_FORMAT;
991 return wdstrategy(bp);
992 }
993 #endif
994
995 int
996 wdsize(dev)
997 dev_t dev;
998 {
999 struct wd_softc *wd;
1000 int part, unit, omask;
1001 int size;
1002
1003 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1004
1005 unit = WDUNIT(dev);
1006 if (unit >= wd_cd.cd_ndevs)
1007 return (-1);
1008 wd = wd_cd.cd_devs[unit];
1009 if (wd == NULL)
1010 return (-1);
1011
1012 part = WDPART(dev);
1013 omask = wd->sc_dk.dk_openmask & (1 << part);
1014
1015 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1016 return (-1);
1017 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1018 size = -1;
1019 else
1020 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1021 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1022 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1023 return (-1);
1024 return (size);
1025 }
1026
1027 #ifndef __BDEVSW_DUMP_OLD_TYPE
1028 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1029 static int wddoingadump = 0;
1030 static int wddumprecalibrated = 0;
1031 static int wddumpmulti = 1;
1032
1033 /*
1034 * Dump core after a system crash.
1035 */
1036 int
1037 wddump(dev, blkno, va, size)
1038 dev_t dev;
1039 daddr_t blkno;
1040 caddr_t va;
1041 size_t size;
1042 {
1043 struct wd_softc *wd; /* disk unit to do the I/O */
1044 struct disklabel *lp; /* disk's disklabel */
1045 int unit, part;
1046 int nblks; /* total number of sectors left to write */
1047 int err;
1048 char errbuf[256];
1049
1050 /* Check if recursive dump; if so, punt. */
1051 if (wddoingadump)
1052 return EFAULT;
1053 wddoingadump = 1;
1054
1055 unit = WDUNIT(dev);
1056 if (unit >= wd_cd.cd_ndevs)
1057 return ENXIO;
1058 wd = wd_cd.cd_devs[unit];
1059 if (wd == (struct wd_softc *)0)
1060 return ENXIO;
1061
1062 part = WDPART(dev);
1063
1064 /* Make sure it was initialized. */
1065 if (wd->drvp->state < READY)
1066 return ENXIO;
1067
1068 /* Convert to disk sectors. Request must be a multiple of size. */
1069 lp = wd->sc_dk.dk_label;
1070 if ((size % lp->d_secsize) != 0)
1071 return EFAULT;
1072 nblks = size / lp->d_secsize;
1073 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1074
1075 /* Check transfer bounds against partition size. */
1076 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1077 return EINVAL;
1078
1079 /* Offset block number to start of partition. */
1080 blkno += lp->d_partitions[part].p_offset;
1081
1082 /* Recalibrate, if first dump transfer. */
1083 if (wddumprecalibrated == 0) {
1084 wddumpmulti = wd->sc_multi;
1085 wddumprecalibrated = 1;
1086 wd->drvp->state = RECAL;
1087 }
1088
1089 while (nblks > 0) {
1090 again:
1091 wd->sc_wdc_bio.blkno = blkno;
1092 wd->sc_wdc_bio.flags = ATA_POLL;
1093 if (wddumpmulti == 1)
1094 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1095 if (wd->sc_flags & WDF_LBA)
1096 wd->sc_wdc_bio.flags |= ATA_LBA;
1097 wd->sc_wdc_bio.bcount =
1098 min(nblks, wddumpmulti) * lp->d_secsize;
1099 wd->sc_wdc_bio.databuf = va;
1100 #ifndef WD_DUMP_NOT_TRUSTED
1101 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1102 case WDC_TRY_AGAIN:
1103 panic("wddump: try again");
1104 break;
1105 case WDC_QUEUED:
1106 panic("wddump: polled command has been queued");
1107 break;
1108 case WDC_COMPLETE:
1109 break;
1110 }
1111 switch(wd->sc_wdc_bio.error) {
1112 case TIMEOUT:
1113 printf("wddump: device timed out");
1114 err = EIO;
1115 break;
1116 case ERR_DF:
1117 printf("wddump: drive fault");
1118 err = EIO;
1119 break;
1120 case ERR_DMA:
1121 printf("wddump: DMA error");
1122 err = EIO;
1123 break;
1124 case ERROR:
1125 errbuf[0] = '\0';
1126 print_wderror(wd->sc_wdc_bio.r_error, errbuf);
1127 printf("wddump: %s", errbuf);
1128 err = EIO;
1129 break;
1130 case NOERROR:
1131 err = 0;
1132 break;
1133 default:
1134 panic("wddump: unknown error type");
1135 }
1136 if (err != 0) {
1137 if (wddumpmulti != 1) {
1138 wddumpmulti = 1; /* retry in single-sector */
1139 printf(", retrying\n");
1140 goto again;
1141 }
1142 printf("\n");
1143 return err;
1144 }
1145 #else /* WD_DUMP_NOT_TRUSTED */
1146 /* Let's just talk about this first... */
1147 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1148 unit, va, cylin, head, sector);
1149 delay(500 * 1000); /* half a second */
1150 #endif
1151
1152 /* update block count */
1153 nblks -= min(nblks, wddumpmulti);
1154 blkno += min(nblks, wddumpmulti);
1155 va += min(nblks, wddumpmulti) * lp->d_secsize;
1156 }
1157
1158 wddoingadump = 0;
1159 return 0;
1160 }
1161 #else /* __BDEVSW_DUMP_NEW_TYPE */
1162
1163
1164 int
1165 wddump(dev, blkno, va, size)
1166 dev_t dev;
1167 daddr_t blkno;
1168 caddr_t va;
1169 size_t size;
1170 {
1171
1172 /* Not implemented. */
1173 return ENXIO;
1174 }
1175 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1176
1177 #ifdef HAS_BAD144_HANDLING
1178 /*
1179 * Internalize the bad sector table.
1180 */
1181 void
1182 bad144intern(wd)
1183 struct wd_softc *wd;
1184 {
1185 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1186 struct disklabel *lp = wd->sc_dk.dk_label;
1187 int i = 0;
1188
1189 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1190
1191 for (; i < NBT_BAD; i++) {
1192 if (bt->bt_bad[i].bt_cyl == 0xffff)
1193 break;
1194 wd->sc_badsect[i] =
1195 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1196 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1197 (bt->bt_bad[i].bt_trksec & 0xff);
1198 }
1199 for (; i < NBT_BAD+1; i++)
1200 wd->sc_badsect[i] = -1;
1201 }
1202 #endif
1203
1204 void
1205 print_wderror(errno, buf)
1206 int errno;
1207 char *buf;
1208 {
1209 static char *errstr[] = {"address mark not found", "track 0 not found",
1210 "aborted command", "media change requested", "id not found",
1211 "media changed", "uncorrectable data error", "bad block detected"};
1212 int i;
1213 char *sep = "";
1214
1215 if (errno == 0) {
1216 sprintf(buf, "error not notified");
1217 }
1218
1219 for (i = 0; i < 8; i++) {
1220 if (errno & (1 << i)) {
1221 buf += sprintf(buf, "%s %s", sep, errstr[i]);
1222 sep = ",";
1223 }
1224 }
1225 }
1226
1227 int
1228 wd_get_params(wd, flags, params)
1229 struct wd_softc *wd;
1230 u_int8_t flags;
1231 struct ataparams *params;
1232 {
1233 switch (ata_get_params(wd->drvp, flags, params)) {
1234 case CMD_AGAIN:
1235 return 1;
1236 case CMD_ERR:
1237 /*
1238 * We `know' there's a drive here; just assume it's old.
1239 * This geometry is only used to read the MBR and print a
1240 * (false) attach message.
1241 */
1242 strncpy(params->atap_model, "ST506",
1243 sizeof params->atap_model);
1244 params->atap_config = ATA_CFG_FIXED;
1245 params->atap_cylinders = 1024;
1246 params->atap_heads = 8;
1247 params->atap_sectors = 17;
1248 params->atap_multi = 1;
1249 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1250 return 0;
1251 case CMD_OK:
1252 return 0;
1253 default:
1254 panic("wd_get_params: bad return code from ata_get_params");
1255 /* NOTREACHED */
1256 }
1257 }
1258
1259 /*
1260 * Allocate space for a ioctl queue structure. Mostly taken from
1261 * scsipi_ioctl.c
1262 */
1263 struct wd_ioctl *
1264 wi_get()
1265 {
1266 struct wd_ioctl *wi;
1267 int s;
1268
1269 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
1270 memset(wi, 0, sizeof (struct wd_ioctl));
1271 s = splbio();
1272 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1273 splx(s);
1274 return (wi);
1275 }
1276
1277 /*
1278 * Free an ioctl structure and remove it from our list
1279 */
1280
1281 void
1282 wi_free(wi)
1283 struct wd_ioctl *wi;
1284 {
1285 int s;
1286
1287 s = splbio();
1288 LIST_REMOVE(wi, wi_list);
1289 splx(s);
1290 free(wi, M_TEMP);
1291 }
1292
1293 /*
1294 * Find a wd_ioctl structure based on the struct buf.
1295 */
1296
1297 struct wd_ioctl *
1298 wi_find(bp)
1299 struct buf *bp;
1300 {
1301 struct wd_ioctl *wi;
1302 int s;
1303
1304 s = splbio();
1305 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1306 if (bp == &wi->wi_bp)
1307 break;
1308 splx(s);
1309 return (wi);
1310 }
1311
1312 /*
1313 * Ioctl pseudo strategy routine
1314 *
1315 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1316 * happens here is:
1317 *
1318 * - wdioctl() queues a wd_ioctl structure.
1319 *
1320 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1321 * user space I/O is required. If physio() is called, physio() eventually
1322 * calls wdioctlstrategy().
1323 *
1324 * - In either case, wdioctlstrategy() calls wdc_exec_command()
1325 * to perform the actual command
1326 *
1327 * The reason for the use of the pseudo strategy routine is because
1328 * when doing I/O to/from user space, physio _really_ wants to be in
1329 * the loop. We could put the entire buffer into the ioctl request
1330 * structure, but that won't scale if we want to do things like download
1331 * microcode.
1332 */
1333
1334 void
1335 wdioctlstrategy(bp)
1336 struct buf *bp;
1337 {
1338 struct wd_ioctl *wi;
1339 struct wdc_command wdc_c;
1340 int error = 0;
1341
1342 wi = wi_find(bp);
1343 if (wi == NULL) {
1344 printf("user_strat: No ioctl\n");
1345 error = EINVAL;
1346 goto bad;
1347 }
1348
1349 memset(&wdc_c, 0, sizeof(wdc_c));
1350
1351 /*
1352 * Abort if physio broke up the transfer
1353 */
1354
1355 if (bp->b_bcount != wi->wi_atareq.datalen) {
1356 printf("physio split wd ioctl request... cannot proceed\n");
1357 error = EIO;
1358 goto bad;
1359 }
1360
1361 /*
1362 * Abort if we didn't get a buffer size that was a multiple of
1363 * our sector size (or was larger than NBBY)
1364 */
1365
1366 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1367 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1368 (1 << NBBY)) {
1369 error = EINVAL;
1370 goto bad;
1371 }
1372
1373 /*
1374 * Make sure a timeout was supplied in the ioctl request
1375 */
1376
1377 if (wi->wi_atareq.timeout == 0) {
1378 error = EINVAL;
1379 goto bad;
1380 }
1381
1382 if (wi->wi_atareq.flags & ATACMD_READ)
1383 wdc_c.flags |= AT_READ;
1384 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1385 wdc_c.flags |= AT_WRITE;
1386
1387 wdc_c.flags |= AT_WAIT;
1388
1389 wdc_c.timeout = wi->wi_atareq.timeout;
1390 wdc_c.r_command = wi->wi_atareq.command;
1391 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1392 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1393 wdc_c.r_sector = wi->wi_atareq.sec_num;
1394 wdc_c.r_count = wi->wi_atareq.sec_count;
1395 wdc_c.r_precomp = wi->wi_atareq.features;
1396 wdc_c.r_st_bmask = WDCS_DRDY;
1397 wdc_c.r_st_pmask = WDCS_DRDY;
1398 wdc_c.data = wi->wi_bp.b_data;
1399 wdc_c.bcount = wi->wi_bp.b_bcount;
1400
1401 if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
1402 wi->wi_atareq.retsts = ATACMD_ERROR;
1403 goto bad;
1404 }
1405
1406
1407 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1408 if (wdc_c.flags & AT_ERROR) {
1409 wi->wi_atareq.retsts = ATACMD_ERROR;
1410 wi->wi_atareq.error = wdc_c.r_error;
1411 } else if (wdc_c.flags & AT_DF)
1412 wi->wi_atareq.retsts = ATACMD_DF;
1413 else
1414 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1415 } else
1416 wi->wi_atareq.retsts = ATACMD_OK;
1417
1418 bp->b_error = 0;
1419 biodone(bp);
1420 return;
1421 bad:
1422 bp->b_flags |= B_ERROR;
1423 bp->b_error = error;
1424 biodone(bp);
1425 }
1426