wd.c revision 1.193 1 /* $NetBSD: wd.c,v 1.193 1999/02/28 17:15:27 explorer 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 break;
474 case WDC_COMPLETE:
475 wddone(wd);
476 break;
477 default:
478 panic("__wdstart: bad return code from wdc_ata_bio()");
479 }
480 }
481
482 void
483 wddone(v)
484 void *v;
485 {
486 struct wd_softc *wd = v;
487 struct buf *bp = wd->sc_bp;
488 char buf[256], *errbuf = buf;
489 WDCDEBUG_PRINT(("wddone %s\n", wd->sc_dev.dv_xname),
490 DEBUG_XFERS);
491
492 bp->b_resid = wd->sc_wdc_bio.bcount;
493 errbuf[0] = '\0';
494 switch (wd->sc_wdc_bio.error) {
495 case ERR_DMA:
496 errbuf = "DMA error";
497 goto retry;
498 case ERR_DF:
499 errbuf = "device fault";
500 goto retry;
501 case TIMEOUT:
502 errbuf = "device timeout";
503 goto retry;
504 case ERROR:
505 /* Don't care about media change bits */
506 if (wd->sc_wdc_bio.r_error != 0 &&
507 (wd->sc_wdc_bio.r_error & ~(WDCE_MC | WDCE_MCR)) == 0)
508 goto noerror;
509 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
510 retry: /* Just reset and retry. Can we do more ? */
511 wdc_reset_channel(wd->drvp);
512 diskerr(bp, "wd", errbuf, LOG_PRINTF,
513 wd->sc_wdc_bio.blkdone, wd->sc_dk.dk_label);
514 if (wd->retries++ < WDIORETRIES) {
515 printf(", retrying\n");
516 timeout(wdrestart, wd, RECOVERYTIME);
517 return;
518 }
519 printf("\n");
520 bp->b_flags |= B_ERROR;
521 bp->b_error = EIO;
522 break;
523 case NOERROR:
524 noerror: if ((wd->sc_wdc_bio.flags & ATA_CORR) || wd->retries > 0)
525 printf("%s: soft error (corrected)\n",
526 wd->sc_dev.dv_xname);
527 }
528 disk_unbusy(&wd->sc_dk, (bp->b_bcount - bp->b_resid));
529 #if NRND > 0
530 rnd_add_uint32(&wd->rnd_source, bp->b_blkno);
531 #endif
532 biodone(bp);
533 wd->openings++;
534 wdstart(wd);
535 }
536
537 void
538 wdrestart(v)
539 void *v;
540 {
541 struct wd_softc *wd = v;
542 struct buf *bp = wd->sc_bp;
543 int s;
544 WDCDEBUG_PRINT(("wdrestart %s\n", wd->sc_dev.dv_xname),
545 DEBUG_XFERS);
546
547 s = splbio();
548 __wdstart(v, bp);
549 splx(s);
550 }
551
552 int
553 wdread(dev, uio, flags)
554 dev_t dev;
555 struct uio *uio;
556 int flags;
557 {
558
559 WDCDEBUG_PRINT(("wdread\n"), DEBUG_XFERS);
560 return (physio(wdstrategy, NULL, dev, B_READ, minphys, uio));
561 }
562
563 int
564 wdwrite(dev, uio, flags)
565 dev_t dev;
566 struct uio *uio;
567 int flags;
568 {
569
570 WDCDEBUG_PRINT(("wdwrite\n"), DEBUG_XFERS);
571 return (physio(wdstrategy, NULL, dev, B_WRITE, minphys, uio));
572 }
573
574 /*
575 * Wait interruptibly for an exclusive lock.
576 *
577 * XXX
578 * Several drivers do this; it should be abstracted and made MP-safe.
579 */
580 int
581 wdlock(wd)
582 struct wd_softc *wd;
583 {
584 int error;
585 int s;
586
587 WDCDEBUG_PRINT(("wdlock\n"), DEBUG_FUNCS);
588
589 s = splbio();
590
591 while ((wd->sc_flags & WDF_LOCKED) != 0) {
592 wd->sc_flags |= WDF_WANTED;
593 if ((error = tsleep(wd, PRIBIO | PCATCH,
594 "wdlck", 0)) != 0) {
595 splx(s);
596 return error;
597 }
598 }
599 wd->sc_flags |= WDF_LOCKED;
600 splx(s);
601 return 0;
602 }
603
604 /*
605 * Unlock and wake up any waiters.
606 */
607 void
608 wdunlock(wd)
609 struct wd_softc *wd;
610 {
611
612 WDCDEBUG_PRINT(("wdunlock\n"), DEBUG_FUNCS);
613
614 wd->sc_flags &= ~WDF_LOCKED;
615 if ((wd->sc_flags & WDF_WANTED) != 0) {
616 wd->sc_flags &= ~WDF_WANTED;
617 wakeup(wd);
618 }
619 }
620
621 int
622 wdopen(dev, flag, fmt, p)
623 dev_t dev;
624 int flag, fmt;
625 struct proc *p;
626 {
627 struct wd_softc *wd;
628 int unit, part;
629 int error;
630
631 WDCDEBUG_PRINT(("wdopen\n"), DEBUG_FUNCS);
632 unit = WDUNIT(dev);
633 if (unit >= wd_cd.cd_ndevs)
634 return ENXIO;
635 wd = wd_cd.cd_devs[unit];
636 if (wd == NULL)
637 return ENXIO;
638
639 /*
640 * If this is the first open of this device, add a reference
641 * to the adapter.
642 */
643 if (wd->sc_dk.dk_openmask == 0 &&
644 (error = wdc_ata_addref(wd->drvp)) != 0)
645 return (error);
646
647 if ((error = wdlock(wd)) != 0)
648 goto bad4;
649
650 if (wd->sc_dk.dk_openmask != 0) {
651 /*
652 * If any partition is open, but the disk has been invalidated,
653 * disallow further opens.
654 */
655 if ((wd->sc_flags & WDF_LOADED) == 0) {
656 error = EIO;
657 goto bad3;
658 }
659 } else {
660 if ((wd->sc_flags & WDF_LOADED) == 0) {
661 wd->sc_flags |= WDF_LOADED;
662
663 /* Load the physical device parameters. */
664 wd_get_params(wd, AT_WAIT, &wd->sc_params);
665
666 /* Load the partition info if not already loaded. */
667 wdgetdisklabel(wd);
668 }
669 }
670
671 part = WDPART(dev);
672
673 /* Check that the partition exists. */
674 if (part != RAW_PART &&
675 (part >= wd->sc_dk.dk_label->d_npartitions ||
676 wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
677 error = ENXIO;
678 goto bad;
679 }
680
681 /* Insure only one open at a time. */
682 switch (fmt) {
683 case S_IFCHR:
684 wd->sc_dk.dk_copenmask |= (1 << part);
685 break;
686 case S_IFBLK:
687 wd->sc_dk.dk_bopenmask |= (1 << part);
688 break;
689 }
690 wd->sc_dk.dk_openmask =
691 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
692
693 wdunlock(wd);
694 return 0;
695
696 bad:
697 if (wd->sc_dk.dk_openmask == 0) {
698 }
699
700 bad3:
701 wdunlock(wd);
702 bad4:
703 if (wd->sc_dk.dk_openmask == 0)
704 wdc_ata_delref(wd->drvp);
705 return error;
706 }
707
708 int
709 wdclose(dev, flag, fmt, p)
710 dev_t dev;
711 int flag, fmt;
712 struct proc *p;
713 {
714 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
715 int part = WDPART(dev);
716 int error;
717
718 WDCDEBUG_PRINT(("wdclose\n"), DEBUG_FUNCS);
719 if ((error = wdlock(wd)) != 0)
720 return error;
721
722 switch (fmt) {
723 case S_IFCHR:
724 wd->sc_dk.dk_copenmask &= ~(1 << part);
725 break;
726 case S_IFBLK:
727 wd->sc_dk.dk_bopenmask &= ~(1 << part);
728 break;
729 }
730 wd->sc_dk.dk_openmask =
731 wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
732
733 if (wd->sc_dk.dk_openmask == 0) {
734 wd_flushcache(wd,0);
735 /* XXXX Must wait for I/O to complete! */
736
737 wdc_ata_delref(wd->drvp);
738 }
739
740 wdunlock(wd);
741 return 0;
742 }
743
744 void
745 wdgetdefaultlabel(wd, lp)
746 struct wd_softc *wd;
747 struct disklabel *lp;
748 {
749
750 WDCDEBUG_PRINT(("wdgetdefaultlabel\n"), DEBUG_FUNCS);
751 memset(lp, 0, sizeof(struct disklabel));
752
753 lp->d_secsize = DEV_BSIZE;
754 lp->d_ntracks = wd->sc_params.atap_heads;
755 lp->d_nsectors = wd->sc_params.atap_sectors;
756 lp->d_ncylinders = wd->sc_params.atap_cylinders;
757 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
758
759 #if 0
760 if (strcmp(wd->sc_params.atap_model, "ST506") == 0) {
761 lp->d_type = DTYPE_ST506;
762 strncpy(lp->d_typename, "ST506 disk", 16);
763 } else {
764 lp->d_type = DTYPE_ESDI;
765 strncpy(lp->d_typename, "ESDI/IDE",
766 sizeof lp->d_typename);
767 }
768 #endif
769 strncpy(lp->d_typename, wd->sc_params.atap_model, 16);
770 strncpy(lp->d_packname, "fictitious", 16);
771 lp->d_secperunit = wd->sc_capacity;
772 lp->d_rpm = 3600;
773 lp->d_interleave = 1;
774 lp->d_flags = 0;
775
776 lp->d_partitions[RAW_PART].p_offset = 0;
777 lp->d_partitions[RAW_PART].p_size =
778 lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
779 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
780 lp->d_npartitions = RAW_PART + 1;
781
782 lp->d_magic = DISKMAGIC;
783 lp->d_magic2 = DISKMAGIC;
784 lp->d_checksum = dkcksum(lp);
785 }
786
787 /*
788 * Fabricate a default disk label, and try to read the correct one.
789 */
790 void
791 wdgetdisklabel(wd)
792 struct wd_softc *wd;
793 {
794 struct disklabel *lp = wd->sc_dk.dk_label;
795 char *errstring;
796
797 WDCDEBUG_PRINT(("wdgetdisklabel\n"), DEBUG_FUNCS);
798
799 memset(wd->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
800
801 wdgetdefaultlabel(wd, lp);
802
803 wd->sc_badsect[0] = -1;
804
805 if (wd->drvp->state > RECAL)
806 wd->drvp->drive_flags |= DRIVE_RESET;
807 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit, RAW_PART),
808 wdstrategy, lp, wd->sc_dk.dk_cpulabel);
809 if (errstring) {
810 /*
811 * This probably happened because the drive's default
812 * geometry doesn't match the DOS geometry. We
813 * assume the DOS geometry is now in the label and try
814 * again. XXX This is a kluge.
815 */
816 if (wd->drvp->state > RECAL)
817 wd->drvp->drive_flags |= DRIVE_RESET;
818 errstring = readdisklabel(MAKEWDDEV(0, wd->sc_dev.dv_unit,
819 RAW_PART), wdstrategy, lp, wd->sc_dk.dk_cpulabel);
820 }
821 if (errstring) {
822 printf("%s: %s\n", wd->sc_dev.dv_xname, errstring);
823 return;
824 }
825
826 if (wd->drvp->state > RECAL)
827 wd->drvp->drive_flags |= DRIVE_RESET;
828 #ifdef HAS_BAD144_HANDLING
829 if ((lp->d_flags & D_BADSECT) != 0)
830 bad144intern(wd);
831 #endif
832 }
833
834 int
835 wdioctl(dev, xfer, addr, flag, p)
836 dev_t dev;
837 u_long xfer;
838 caddr_t addr;
839 int flag;
840 struct proc *p;
841 {
842 struct wd_softc *wd = wd_cd.cd_devs[WDUNIT(dev)];
843 int error;
844
845 WDCDEBUG_PRINT(("wdioctl\n"), DEBUG_FUNCS);
846
847 if ((wd->sc_flags & WDF_LOADED) == 0)
848 return EIO;
849
850 switch (xfer) {
851 #ifdef HAS_BAD144_HANDLING
852 case DIOCSBAD:
853 if ((flag & FWRITE) == 0)
854 return EBADF;
855 wd->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
856 wd->sc_dk.dk_label->d_flags |= D_BADSECT;
857 bad144intern(wd);
858 return 0;
859 #endif
860
861 case DIOCGDINFO:
862 *(struct disklabel *)addr = *(wd->sc_dk.dk_label);
863 return 0;
864
865 case DIOCGPART:
866 ((struct partinfo *)addr)->disklab = wd->sc_dk.dk_label;
867 ((struct partinfo *)addr)->part =
868 &wd->sc_dk.dk_label->d_partitions[WDPART(dev)];
869 return 0;
870
871 case DIOCWDINFO:
872 case DIOCSDINFO:
873 if ((flag & FWRITE) == 0)
874 return EBADF;
875
876 if ((error = wdlock(wd)) != 0)
877 return error;
878 wd->sc_flags |= WDF_LABELLING;
879
880 error = setdisklabel(wd->sc_dk.dk_label,
881 (struct disklabel *)addr, /*wd->sc_dk.dk_openmask : */0,
882 wd->sc_dk.dk_cpulabel);
883 if (error == 0) {
884 if (wd->drvp->state > RECAL)
885 wd->drvp->drive_flags |= DRIVE_RESET;
886 if (xfer == DIOCWDINFO)
887 error = writedisklabel(WDLABELDEV(dev),
888 wdstrategy, wd->sc_dk.dk_label,
889 wd->sc_dk.dk_cpulabel);
890 }
891
892 wd->sc_flags &= ~WDF_LABELLING;
893 wdunlock(wd);
894 return error;
895
896 case DIOCWLABEL:
897 if ((flag & FWRITE) == 0)
898 return EBADF;
899 if (*(int *)addr)
900 wd->sc_flags |= WDF_WLABEL;
901 else
902 wd->sc_flags &= ~WDF_WLABEL;
903 return 0;
904
905 case DIOCGDEFLABEL:
906 wdgetdefaultlabel(wd, (struct disklabel *)addr);
907 return 0;
908
909 #ifdef notyet
910 case DIOCWFORMAT:
911 if ((flag & FWRITE) == 0)
912 return EBADF;
913 {
914 register struct format_op *fop;
915 struct iovec aiov;
916 struct uio auio;
917
918 fop = (struct format_op *)addr;
919 aiov.iov_base = fop->df_buf;
920 aiov.iov_len = fop->df_count;
921 auio.uio_iov = &aiov;
922 auio.uio_iovcnt = 1;
923 auio.uio_resid = fop->df_count;
924 auio.uio_segflg = 0;
925 auio.uio_offset =
926 fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
927 auio.uio_procp = p;
928 error = physio(wdformat, NULL, dev, B_WRITE, minphys,
929 &auio);
930 fop->df_count -= auio.uio_resid;
931 fop->df_reg[0] = wdc->sc_status;
932 fop->df_reg[1] = wdc->sc_error;
933 return error;
934 }
935 #endif
936
937 case ATAIOCCOMMAND:
938 /*
939 * Make sure this command is (relatively) safe first
940 */
941 if ((((atareq_t *) addr)->flags & ATACMD_READ) == 0 &&
942 (flag & FWRITE) == 0)
943 return (EBADF);
944 {
945 struct wd_ioctl *wi;
946 atareq_t *atareq = (atareq_t *) addr;
947 int error;
948
949 wi = wi_get();
950 wi->wi_softc = wd;
951 wi->wi_atareq = *atareq;
952
953 if (atareq->datalen && atareq->flags &
954 (ATACMD_READ | ATACMD_WRITE)) {
955 wi->wi_iov.iov_base = atareq->databuf;
956 wi->wi_iov.iov_len = atareq->datalen;
957 wi->wi_uio.uio_iov = &wi->wi_iov;
958 wi->wi_uio.uio_iovcnt = 1;
959 wi->wi_uio.uio_resid = atareq->datalen;
960 wi->wi_uio.uio_offset = 0;
961 wi->wi_uio.uio_segflg = UIO_USERSPACE;
962 wi->wi_uio.uio_rw =
963 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE;
964 wi->wi_uio.uio_procp = p;
965 error = physio(wdioctlstrategy, &wi->wi_bp, dev,
966 (atareq->flags & ATACMD_READ) ? B_READ : B_WRITE,
967 minphys, &wi->wi_uio);
968 } else {
969 /* No need to call physio if we don't have any
970 user data */
971 wi->wi_bp.b_flags = 0;
972 wi->wi_bp.b_data = 0;
973 wi->wi_bp.b_bcount = 0;
974 wi->wi_bp.b_dev = 0;
975 wi->wi_bp.b_proc = p;
976 wdioctlstrategy(&wi->wi_bp);
977 error = wi->wi_bp.b_error;
978 }
979 *atareq = wi->wi_atareq;
980 wi_free(wi);
981 return(error);
982 }
983
984 default:
985 return ENOTTY;
986 }
987
988 #ifdef DIAGNOSTIC
989 panic("wdioctl: impossible");
990 #endif
991 }
992
993 #ifdef B_FORMAT
994 int
995 wdformat(struct buf *bp)
996 {
997
998 bp->b_flags |= B_FORMAT;
999 return wdstrategy(bp);
1000 }
1001 #endif
1002
1003 int
1004 wdsize(dev)
1005 dev_t dev;
1006 {
1007 struct wd_softc *wd;
1008 int part, unit, omask;
1009 int size;
1010
1011 WDCDEBUG_PRINT(("wdsize\n"), DEBUG_FUNCS);
1012
1013 unit = WDUNIT(dev);
1014 if (unit >= wd_cd.cd_ndevs)
1015 return (-1);
1016 wd = wd_cd.cd_devs[unit];
1017 if (wd == NULL)
1018 return (-1);
1019
1020 part = WDPART(dev);
1021 omask = wd->sc_dk.dk_openmask & (1 << part);
1022
1023 if (omask == 0 && wdopen(dev, 0, S_IFBLK, NULL) != 0)
1024 return (-1);
1025 if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
1026 size = -1;
1027 else
1028 size = wd->sc_dk.dk_label->d_partitions[part].p_size *
1029 (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
1030 if (omask == 0 && wdclose(dev, 0, S_IFBLK, NULL) != 0)
1031 return (-1);
1032 return (size);
1033 }
1034
1035 #ifndef __BDEVSW_DUMP_OLD_TYPE
1036 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
1037 static int wddoingadump = 0;
1038 static int wddumprecalibrated = 0;
1039 static int wddumpmulti = 1;
1040
1041 /*
1042 * Dump core after a system crash.
1043 */
1044 int
1045 wddump(dev, blkno, va, size)
1046 dev_t dev;
1047 daddr_t blkno;
1048 caddr_t va;
1049 size_t size;
1050 {
1051 struct wd_softc *wd; /* disk unit to do the I/O */
1052 struct disklabel *lp; /* disk's disklabel */
1053 int unit, part;
1054 int nblks; /* total number of sectors left to write */
1055 int err;
1056 char errbuf[256];
1057
1058 /* Check if recursive dump; if so, punt. */
1059 if (wddoingadump)
1060 return EFAULT;
1061 wddoingadump = 1;
1062
1063 unit = WDUNIT(dev);
1064 if (unit >= wd_cd.cd_ndevs)
1065 return ENXIO;
1066 wd = wd_cd.cd_devs[unit];
1067 if (wd == (struct wd_softc *)0)
1068 return ENXIO;
1069
1070 part = WDPART(dev);
1071
1072 /* Make sure it was initialized. */
1073 if (wd->drvp->state < READY)
1074 return ENXIO;
1075
1076 /* Convert to disk sectors. Request must be a multiple of size. */
1077 lp = wd->sc_dk.dk_label;
1078 if ((size % lp->d_secsize) != 0)
1079 return EFAULT;
1080 nblks = size / lp->d_secsize;
1081 blkno = blkno / (lp->d_secsize / DEV_BSIZE);
1082
1083 /* Check transfer bounds against partition size. */
1084 if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
1085 return EINVAL;
1086
1087 /* Offset block number to start of partition. */
1088 blkno += lp->d_partitions[part].p_offset;
1089
1090 /* Recalibrate, if first dump transfer. */
1091 if (wddumprecalibrated == 0) {
1092 wddumpmulti = wd->sc_multi;
1093 wddumprecalibrated = 1;
1094 wd->drvp->state = RECAL;
1095 }
1096
1097 while (nblks > 0) {
1098 again:
1099 wd->sc_wdc_bio.blkno = blkno;
1100 wd->sc_wdc_bio.flags = ATA_POLL;
1101 if (wddumpmulti == 1)
1102 wd->sc_wdc_bio.flags |= ATA_SINGLE;
1103 if (wd->sc_flags & WDF_LBA)
1104 wd->sc_wdc_bio.flags |= ATA_LBA;
1105 wd->sc_wdc_bio.bcount =
1106 min(nblks, wddumpmulti) * lp->d_secsize;
1107 wd->sc_wdc_bio.databuf = va;
1108 #ifndef WD_DUMP_NOT_TRUSTED
1109 switch (wdc_ata_bio(wd->drvp, &wd->sc_wdc_bio)) {
1110 case WDC_TRY_AGAIN:
1111 panic("wddump: try again");
1112 break;
1113 case WDC_QUEUED:
1114 panic("wddump: polled command has been queued");
1115 break;
1116 case WDC_COMPLETE:
1117 break;
1118 }
1119 switch(wd->sc_wdc_bio.error) {
1120 case TIMEOUT:
1121 printf("wddump: device timed out");
1122 err = EIO;
1123 break;
1124 case ERR_DF:
1125 printf("wddump: drive fault");
1126 err = EIO;
1127 break;
1128 case ERR_DMA:
1129 printf("wddump: DMA error");
1130 err = EIO;
1131 break;
1132 case ERROR:
1133 errbuf[0] = '\0';
1134 ata_perror(wd->drvp, wd->sc_wdc_bio.r_error, errbuf);
1135 printf("wddump: %s", errbuf);
1136 err = EIO;
1137 break;
1138 case NOERROR:
1139 err = 0;
1140 break;
1141 default:
1142 panic("wddump: unknown error type");
1143 }
1144 if (err != 0) {
1145 if (wddumpmulti != 1) {
1146 wddumpmulti = 1; /* retry in single-sector */
1147 printf(", retrying\n");
1148 goto again;
1149 }
1150 printf("\n");
1151 return err;
1152 }
1153 #else /* WD_DUMP_NOT_TRUSTED */
1154 /* Let's just talk about this first... */
1155 printf("wd%d: dump addr 0x%x, cylin %d, head %d, sector %d\n",
1156 unit, va, cylin, head, sector);
1157 delay(500 * 1000); /* half a second */
1158 #endif
1159
1160 /* update block count */
1161 nblks -= min(nblks, wddumpmulti);
1162 blkno += min(nblks, wddumpmulti);
1163 va += min(nblks, wddumpmulti) * lp->d_secsize;
1164 }
1165
1166 wddoingadump = 0;
1167 return 0;
1168 }
1169 #else /* __BDEVSW_DUMP_NEW_TYPE */
1170
1171
1172 int
1173 wddump(dev, blkno, va, size)
1174 dev_t dev;
1175 daddr_t blkno;
1176 caddr_t va;
1177 size_t size;
1178 {
1179
1180 /* Not implemented. */
1181 return ENXIO;
1182 }
1183 #endif /* __BDEVSW_DUMP_NEW_TYPE */
1184
1185 #ifdef HAS_BAD144_HANDLING
1186 /*
1187 * Internalize the bad sector table.
1188 */
1189 void
1190 bad144intern(wd)
1191 struct wd_softc *wd;
1192 {
1193 struct dkbad *bt = &wd->sc_dk.dk_cpulabel->bad;
1194 struct disklabel *lp = wd->sc_dk.dk_label;
1195 int i = 0;
1196
1197 WDCDEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
1198
1199 for (; i < NBT_BAD; i++) {
1200 if (bt->bt_bad[i].bt_cyl == 0xffff)
1201 break;
1202 wd->sc_badsect[i] =
1203 bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
1204 (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
1205 (bt->bt_bad[i].bt_trksec & 0xff);
1206 }
1207 for (; i < NBT_BAD+1; i++)
1208 wd->sc_badsect[i] = -1;
1209 }
1210 #endif
1211
1212 int
1213 wd_get_params(wd, flags, params)
1214 struct wd_softc *wd;
1215 u_int8_t flags;
1216 struct ataparams *params;
1217 {
1218 switch (ata_get_params(wd->drvp, flags, params)) {
1219 case CMD_AGAIN:
1220 return 1;
1221 case CMD_ERR:
1222 /*
1223 * We `know' there's a drive here; just assume it's old.
1224 * This geometry is only used to read the MBR and print a
1225 * (false) attach message.
1226 */
1227 strncpy(params->atap_model, "ST506",
1228 sizeof params->atap_model);
1229 params->atap_config = ATA_CFG_FIXED;
1230 params->atap_cylinders = 1024;
1231 params->atap_heads = 8;
1232 params->atap_sectors = 17;
1233 params->atap_multi = 1;
1234 params->atap_capabilities1 = params->atap_capabilities2 = 0;
1235 wd->drvp->ata_vers = -1; /* Mark it as pre-ATA */
1236 return 0;
1237 case CMD_OK:
1238 return 0;
1239 default:
1240 panic("wd_get_params: bad return code from ata_get_params");
1241 /* NOTREACHED */
1242 }
1243 }
1244
1245 void
1246 wd_flushcache(wd, flags)
1247 struct wd_softc *wd;
1248 int flags;
1249 {
1250 struct wdc_command wdc_c;
1251
1252 if (wd->drvp->ata_vers < 4) /* WDCC_FLUSHCACHE is here since ATA-4 */
1253 return;
1254 memset(&wdc_c, 0, sizeof(struct wdc_command));
1255 wdc_c.r_command = WDCC_FLUSHCACHE;
1256 wdc_c.r_st_bmask = WDCS_DRDY;
1257 wdc_c.r_st_pmask = WDCS_DRDY;
1258 wdc_c.flags = flags | AT_WAIT;
1259 wdc_c.timeout = 30000; /* 30s timeout */
1260 if (wdc_exec_command(wd->drvp, &wdc_c) != WDC_COMPLETE) {
1261 printf("%s: flush cache command didn't complete\n",
1262 wd->sc_dev.dv_xname);
1263 }
1264 if (wdc_c.flags & AT_TIMEOU) {
1265 printf("%s: flush cache command timeout\n",
1266 wd->sc_dev.dv_xname);
1267 }
1268 if (wdc_c.flags & AT_DF) {
1269 printf("%s: flush cache command: drive fault\n",
1270 wd->sc_dev.dv_xname);
1271 }
1272 /*
1273 * Ignore error register, it shouldn't report anything else
1274 * than COMMAND ABORTED, which means the device doesn't support
1275 * flush cache
1276 */
1277 }
1278
1279 void
1280 wd_shutdown(arg)
1281 void *arg;
1282 {
1283 struct wd_softc *wd = arg;
1284 wd_flushcache(wd, ATA_POLL);
1285 }
1286
1287 /*
1288 * Allocate space for a ioctl queue structure. Mostly taken from
1289 * scsipi_ioctl.c
1290 */
1291 struct wd_ioctl *
1292 wi_get()
1293 {
1294 struct wd_ioctl *wi;
1295 int s;
1296
1297 wi = malloc(sizeof(struct wd_ioctl), M_TEMP, M_WAITOK);
1298 memset(wi, 0, sizeof (struct wd_ioctl));
1299 s = splbio();
1300 LIST_INSERT_HEAD(&wi_head, wi, wi_list);
1301 splx(s);
1302 return (wi);
1303 }
1304
1305 /*
1306 * Free an ioctl structure and remove it from our list
1307 */
1308
1309 void
1310 wi_free(wi)
1311 struct wd_ioctl *wi;
1312 {
1313 int s;
1314
1315 s = splbio();
1316 LIST_REMOVE(wi, wi_list);
1317 splx(s);
1318 free(wi, M_TEMP);
1319 }
1320
1321 /*
1322 * Find a wd_ioctl structure based on the struct buf.
1323 */
1324
1325 struct wd_ioctl *
1326 wi_find(bp)
1327 struct buf *bp;
1328 {
1329 struct wd_ioctl *wi;
1330 int s;
1331
1332 s = splbio();
1333 for (wi = wi_head.lh_first; wi != 0; wi = wi->wi_list.le_next)
1334 if (bp == &wi->wi_bp)
1335 break;
1336 splx(s);
1337 return (wi);
1338 }
1339
1340 /*
1341 * Ioctl pseudo strategy routine
1342 *
1343 * This is mostly stolen from scsipi_ioctl.c:scsistrategy(). What
1344 * happens here is:
1345 *
1346 * - wdioctl() queues a wd_ioctl structure.
1347 *
1348 * - wdioctl() calls physio/wdioctlstrategy based on whether or not
1349 * user space I/O is required. If physio() is called, physio() eventually
1350 * calls wdioctlstrategy().
1351 *
1352 * - In either case, wdioctlstrategy() calls wdc_exec_command()
1353 * to perform the actual command
1354 *
1355 * The reason for the use of the pseudo strategy routine is because
1356 * when doing I/O to/from user space, physio _really_ wants to be in
1357 * the loop. We could put the entire buffer into the ioctl request
1358 * structure, but that won't scale if we want to do things like download
1359 * microcode.
1360 */
1361
1362 void
1363 wdioctlstrategy(bp)
1364 struct buf *bp;
1365 {
1366 struct wd_ioctl *wi;
1367 struct wdc_command wdc_c;
1368 int error = 0;
1369
1370 wi = wi_find(bp);
1371 if (wi == NULL) {
1372 printf("user_strat: No ioctl\n");
1373 error = EINVAL;
1374 goto bad;
1375 }
1376
1377 memset(&wdc_c, 0, sizeof(wdc_c));
1378
1379 /*
1380 * Abort if physio broke up the transfer
1381 */
1382
1383 if (bp->b_bcount != wi->wi_atareq.datalen) {
1384 printf("physio split wd ioctl request... cannot proceed\n");
1385 error = EIO;
1386 goto bad;
1387 }
1388
1389 /*
1390 * Abort if we didn't get a buffer size that was a multiple of
1391 * our sector size (or was larger than NBBY)
1392 */
1393
1394 if ((bp->b_bcount % wi->wi_softc->sc_dk.dk_label->d_secsize) != 0 ||
1395 (bp->b_bcount / wi->wi_softc->sc_dk.dk_label->d_secsize) >=
1396 (1 << NBBY)) {
1397 error = EINVAL;
1398 goto bad;
1399 }
1400
1401 /*
1402 * Make sure a timeout was supplied in the ioctl request
1403 */
1404
1405 if (wi->wi_atareq.timeout == 0) {
1406 error = EINVAL;
1407 goto bad;
1408 }
1409
1410 if (wi->wi_atareq.flags & ATACMD_READ)
1411 wdc_c.flags |= AT_READ;
1412 else if (wi->wi_atareq.flags & ATACMD_WRITE)
1413 wdc_c.flags |= AT_WRITE;
1414
1415 if (wi->wi_atareq.flags & ATACMD_READREG)
1416 wdc_c.flags |= AT_READREG;
1417
1418 wdc_c.flags |= AT_WAIT;
1419
1420 wdc_c.timeout = wi->wi_atareq.timeout;
1421 wdc_c.r_command = wi->wi_atareq.command;
1422 wdc_c.r_head = wi->wi_atareq.head & 0x0f;
1423 wdc_c.r_cyl = wi->wi_atareq.cylinder;
1424 wdc_c.r_sector = wi->wi_atareq.sec_num;
1425 wdc_c.r_count = wi->wi_atareq.sec_count;
1426 wdc_c.r_precomp = wi->wi_atareq.features;
1427 wdc_c.r_st_bmask = WDCS_DRDY;
1428 wdc_c.r_st_pmask = WDCS_DRDY;
1429 wdc_c.data = wi->wi_bp.b_data;
1430 wdc_c.bcount = wi->wi_bp.b_bcount;
1431
1432 if (wdc_exec_command(wi->wi_softc->drvp, &wdc_c) != WDC_COMPLETE) {
1433 wi->wi_atareq.retsts = ATACMD_ERROR;
1434 goto bad;
1435 }
1436
1437 if (wdc_c.flags & (AT_ERROR | AT_TIMEOU | AT_DF)) {
1438 if (wdc_c.flags & AT_ERROR) {
1439 wi->wi_atareq.retsts = ATACMD_ERROR;
1440 wi->wi_atareq.error = wdc_c.r_error;
1441 } else if (wdc_c.flags & AT_DF)
1442 wi->wi_atareq.retsts = ATACMD_DF;
1443 else
1444 wi->wi_atareq.retsts = ATACMD_TIMEOUT;
1445 } else {
1446 wi->wi_atareq.retsts = ATACMD_OK;
1447 if (wi->wi_atareq.flags & ATACMD_READREG) {
1448 wi->wi_atareq.head = wdc_c.r_head ;
1449 wi->wi_atareq.cylinder = wdc_c.r_cyl;
1450 wi->wi_atareq.sec_num = wdc_c.r_sector;
1451 wi->wi_atareq.sec_count = wdc_c.r_count;
1452 wi->wi_atareq.features = wdc_c.r_precomp;
1453 wi->wi_atareq.error = wdc_c.r_error;
1454 }
1455 }
1456
1457 bp->b_error = 0;
1458 biodone(bp);
1459 return;
1460 bad:
1461 bp->b_flags |= B_ERROR;
1462 bp->b_error = error;
1463 biodone(bp);
1464 }
1465