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