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