Home | History | Annotate | Line # | Download | only in mca
ed_mca.c revision 1.25
      1 /*	$NetBSD: ed_mca.c,v 1.25 2004/09/25 04:47:02 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2001 The NetBSD Foundation, Inc.
      5  *
      6  * This code is derived from software contributed to The NetBSD Foundation
      7  * by Jaromir Dolecek.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *        This product includes software developed by the NetBSD
     20  *        Foundation, Inc. and its contributors.
     21  * 4. The name of the author may not be used to endorse or promote products
     22  *    derived from this software without specific prior written permission.
     23  *
     24  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     25  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     26  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     27  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     28  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     29  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     30  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     31  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     32  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     33  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     34  */
     35 
     36 /*
     37  * Disk drive goo for MCA ESDI controller driver.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: ed_mca.c,v 1.25 2004/09/25 04:47:02 thorpej Exp $");
     42 
     43 #include "rnd.h"
     44 
     45 #include <sys/param.h>
     46 #include <sys/systm.h>
     47 #include <sys/kernel.h>
     48 #include <sys/conf.h>
     49 #include <sys/file.h>
     50 #include <sys/stat.h>
     51 #include <sys/ioctl.h>
     52 #include <sys/buf.h>
     53 #include <sys/uio.h>
     54 #include <sys/malloc.h>
     55 #include <sys/device.h>
     56 #include <sys/disklabel.h>
     57 #include <sys/disk.h>
     58 #include <sys/syslog.h>
     59 #include <sys/proc.h>
     60 #include <sys/vnode.h>
     61 #if NRND > 0
     62 #include <sys/rnd.h>
     63 #endif
     64 
     65 #include <machine/intr.h>
     66 #include <machine/bus.h>
     67 
     68 #include <dev/mca/mcavar.h>
     69 
     70 #include <dev/mca/edcreg.h>
     71 #include <dev/mca/edvar.h>
     72 #include <dev/mca/edcvar.h>
     73 
     74 /* #define ATADEBUG */
     75 
     76 #ifdef ATADEBUG
     77 #define ATADEBUG_PRINT(args, level)  printf args
     78 #else
     79 #define ATADEBUG_PRINT(args, level)
     80 #endif
     81 
     82 #define	EDLABELDEV(dev) (MAKEDISKDEV(major(dev), DISKUNIT(dev), RAW_PART))
     83 
     84 static int     ed_mca_probe   __P((struct device *, struct cfdata *, void *));
     85 static void    ed_mca_attach  __P((struct device *, struct device *, void *));
     86 
     87 CFATTACH_DECL(ed_mca, sizeof(struct ed_softc),
     88     ed_mca_probe, ed_mca_attach, NULL, NULL);
     89 
     90 extern struct cfdriver ed_cd;
     91 
     92 static int	ed_get_params __P((struct ed_softc *, int *));
     93 static void	edgetdisklabel	__P((dev_t, struct ed_softc *));
     94 static void	edgetdefaultlabel __P((struct ed_softc *, struct disklabel *));
     95 
     96 dev_type_open(edmcaopen);
     97 dev_type_close(edmcaclose);
     98 dev_type_read(edmcaread);
     99 dev_type_write(edmcawrite);
    100 dev_type_ioctl(edmcaioctl);
    101 dev_type_strategy(edmcastrategy);
    102 dev_type_dump(edmcadump);
    103 dev_type_size(edmcasize);
    104 
    105 const struct bdevsw ed_bdevsw = {
    106 	edmcaopen, edmcaclose, edmcastrategy, edmcaioctl,
    107 	edmcadump, edmcasize, D_DISK
    108 };
    109 
    110 const struct cdevsw ed_cdevsw = {
    111 	edmcaopen, edmcaclose, edmcaread, edmcawrite, edmcaioctl,
    112 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
    113 };
    114 
    115 static struct dkdriver eddkdriver = { edmcastrategy, minphys };
    116 
    117 /*
    118  * Just check if it's possible to identify the disk.
    119  */
    120 static int
    121 ed_mca_probe(parent, cf, aux)
    122 	struct device *parent;
    123 	struct cfdata *cf;
    124 	void *aux;
    125 {
    126 	u_int16_t cmd_args[2];
    127 	struct edc_mca_softc *sc = (void *) parent;
    128 	struct ed_attach_args *eda = (struct ed_attach_args *) aux;
    129 	int found = 1;
    130 
    131 	/*
    132 	 * Get Device Configuration (09).
    133 	 */
    134 	cmd_args[0] = 14;	/* Options: 00s110, s: 0=Physical 1=Pseudo */
    135 	cmd_args[1] = 0;
    136 	if (edc_run_cmd(sc, CMD_GET_DEV_CONF, eda->edc_drive, cmd_args, 2, 1))
    137 		found = 0;
    138 
    139 	return (found);
    140 }
    141 
    142 static void
    143 ed_mca_attach(parent, self, aux)
    144 	struct device *parent, *self;
    145 	void *aux;
    146 {
    147 	struct ed_softc *ed = (void *) self;
    148 	struct edc_mca_softc *sc = (void *) parent;
    149 	struct ed_attach_args *eda = (struct ed_attach_args *) aux;
    150 	char pbuf[8];
    151 	int drv_flags;
    152 
    153 	ed->edc_softc = sc;
    154 	ed->sc_devno  = eda->edc_drive;
    155 	edc_add_disk(sc, ed);
    156 
    157 	bufq_alloc(&ed->sc_q, BUFQ_DISKSORT|BUFQ_SORT_RAWBLOCK);
    158 	simple_lock_init(&ed->sc_q_lock);
    159 
    160 	if (ed_get_params(ed, &drv_flags)) {
    161 		printf(": IDENTIFY failed, no disk found\n");
    162 		return;
    163 	}
    164 
    165 	format_bytes(pbuf, sizeof(pbuf),
    166 		(u_int64_t) ed->sc_capacity * DEV_BSIZE);
    167 	printf(": %s, %u cyl, %u head, %u sec, 512 bytes/sect x %u sectors\n",
    168 		pbuf,
    169 		ed->cyl, ed->heads, ed->sectors,
    170 		ed->sc_capacity);
    171 
    172 	printf("%s: %u spares/cyl, %s, %s, %s, %s, %s\n",
    173 		ed->sc_dev.dv_xname, ed->spares,
    174 		(drv_flags & (1 << 0)) ? "NoRetries" : "Retries",
    175 		(drv_flags & (1 << 1)) ? "Removable" : "Fixed",
    176 		(drv_flags & (1 << 2)) ? "SkewedFormat" : "NoSkew",
    177 		(drv_flags & (1 << 3)) ? "ZeroDefect" : "Defects",
    178 		(drv_flags & (1 << 4)) ? "InvalidSecondary" : "SecondaryOK"
    179 		);
    180 
    181 	/*
    182 	 * Initialize and attach the disk structure.
    183 	 */
    184 	ed->sc_dk.dk_driver = &eddkdriver;
    185 	ed->sc_dk.dk_name = ed->sc_dev.dv_xname;
    186 	disk_attach(&ed->sc_dk);
    187 #if NRND > 0
    188 	rnd_attach_source(&ed->rnd_source, ed->sc_dev.dv_xname,
    189 			  RND_TYPE_DISK, 0);
    190 #endif
    191 
    192 	ed->sc_flags |= EDF_INIT;
    193 
    194 	/* Discover wedges on this disk. */
    195 	dkwedge_discover(&ed->sc_dk);
    196 }
    197 
    198 /*
    199  * Read/write routine for a buffer.  Validates the arguments and schedules the
    200  * transfer.  Does not wait for the transfer to complete.
    201  */
    202 void
    203 edmcastrategy(bp)
    204 	struct buf *bp;
    205 {
    206 	struct ed_softc *ed = device_lookup(&ed_cd, DISKUNIT(bp->b_dev));
    207 	struct disklabel *lp = ed->sc_dk.dk_label;
    208 	daddr_t blkno;
    209 
    210 	ATADEBUG_PRINT(("edmcastrategy (%s)\n", ed->sc_dev.dv_xname),
    211 	    DEBUG_XFERS);
    212 
    213 	/* Valid request?  */
    214 	if (bp->b_blkno < 0 ||
    215 	    (bp->b_bcount % lp->d_secsize) != 0 ||
    216 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
    217 		bp->b_error = EINVAL;
    218 		goto bad;
    219 	}
    220 
    221 	/* If device invalidated (e.g. media change, door open), error. */
    222 	if ((ed->sc_flags & WDF_LOADED) == 0) {
    223 		bp->b_error = EIO;
    224 		goto bad;
    225 	}
    226 
    227 	/* If it's a null transfer, return immediately. */
    228 	if (bp->b_bcount == 0)
    229 		goto done;
    230 
    231 	/*
    232 	 * Do bounds checking, adjust transfer. if error, process.
    233 	 * If end of partition, just return.
    234 	 */
    235 	if (DISKPART(bp->b_dev) != RAW_PART &&
    236 	    bounds_check_with_label(&ed->sc_dk, bp,
    237 	    (ed->sc_flags & (WDF_WLABEL|WDF_LABELLING)) != 0) <= 0)
    238 		goto done;
    239 
    240 	/*
    241 	 * Now convert the block number to absolute and put it in
    242 	 * terms of the device's logical block size.
    243 	 */
    244 	if (lp->d_secsize >= DEV_BSIZE)
    245 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
    246 	else
    247 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
    248 
    249 	if (DISKPART(bp->b_dev) != RAW_PART)
    250 		blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
    251 
    252 	bp->b_rawblkno = blkno;
    253 
    254 	/* Queue transfer on drive, activate drive and controller if idle. */
    255 	simple_lock(&ed->sc_q_lock);
    256 	BUFQ_PUT(&ed->sc_q, bp);
    257 	simple_unlock(&ed->sc_q_lock);
    258 
    259 	/* Ring the worker thread */
    260 	wakeup_one(ed->edc_softc);
    261 
    262 	return;
    263 bad:
    264 	bp->b_flags |= B_ERROR;
    265 done:
    266 	/* Toss transfer; we're done early. */
    267 	bp->b_resid = bp->b_bcount;
    268 	biodone(bp);
    269 }
    270 
    271 int
    272 edmcaread(dev, uio, flags)
    273 	dev_t dev;
    274 	struct uio *uio;
    275 	int flags;
    276 {
    277 	ATADEBUG_PRINT(("edread\n"), DEBUG_XFERS);
    278 	return (physio(edmcastrategy, NULL, dev, B_READ, minphys, uio));
    279 }
    280 
    281 int
    282 edmcawrite(dev, uio, flags)
    283 	dev_t dev;
    284 	struct uio *uio;
    285 	int flags;
    286 {
    287 	ATADEBUG_PRINT(("edwrite\n"), DEBUG_XFERS);
    288 	return (physio(edmcastrategy, NULL, dev, B_WRITE, minphys, uio));
    289 }
    290 
    291 int
    292 edmcaopen(dev, flag, fmt, p)
    293 	dev_t dev;
    294 	int flag, fmt;
    295 	struct proc *p;
    296 {
    297 	struct ed_softc *wd;
    298 	int part, error;
    299 
    300 	ATADEBUG_PRINT(("edopen\n"), DEBUG_FUNCS);
    301 	wd = device_lookup(&ed_cd, DISKUNIT(dev));
    302 	if (wd == NULL || (wd->sc_flags & EDF_INIT) == 0)
    303 		return (ENXIO);
    304 
    305 	part = DISKPART(dev);
    306 
    307 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    308 		return (error);
    309 
    310 	/*
    311 	 * If there are wedges, and this is not RAW_PART, then we
    312 	 * need to fail.
    313 	 */
    314 	if (wd->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
    315 		error = EBUSY;
    316 		goto bad1;
    317 	}
    318 
    319 	if (wd->sc_dk.dk_openmask != 0) {
    320 		/*
    321 		 * If any partition is open, but the disk has been invalidated,
    322 		 * disallow further opens.
    323 		 */
    324 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    325 			error = EIO;
    326 			goto bad1;
    327 		}
    328 	} else {
    329 		if ((wd->sc_flags & WDF_LOADED) == 0) {
    330 			int s;
    331 
    332 			wd->sc_flags |= WDF_LOADED;
    333 
    334 			/* Load the physical device parameters. */
    335 			s = splbio();
    336 			ed_get_params(wd, NULL);
    337 			splx(s);
    338 
    339 			/* Load the partition info if not already loaded. */
    340 			edgetdisklabel(dev, wd);
    341 		}
    342 	}
    343 
    344 	/* Check that the partition exists. */
    345 	if (part != RAW_PART &&
    346 	    (part >= wd->sc_dk.dk_label->d_npartitions ||
    347 	     wd->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
    348 		error = ENXIO;
    349 		goto bad1;
    350 	}
    351 
    352 	/* Insure only one open at a time. */
    353 	switch (fmt) {
    354 	case S_IFCHR:
    355 		wd->sc_dk.dk_copenmask |= (1 << part);
    356 		break;
    357 	case S_IFBLK:
    358 		wd->sc_dk.dk_bopenmask |= (1 << part);
    359 		break;
    360 	}
    361 	wd->sc_dk.dk_openmask =
    362 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    363 
    364 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    365 	return 0;
    366 
    367  bad1:
    368 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    369 	return (error);
    370 }
    371 
    372 int
    373 edmcaclose(dev, flag, fmt, p)
    374 	dev_t dev;
    375 	int flag, fmt;
    376 	struct proc *p;
    377 {
    378 	struct ed_softc *wd = device_lookup(&ed_cd, DISKUNIT(dev));
    379 	int part = DISKPART(dev);
    380 	int error;
    381 
    382 	ATADEBUG_PRINT(("edmcaclose\n"), DEBUG_FUNCS);
    383 
    384 	if ((error = lockmgr(&wd->sc_dk.dk_openlock, LK_EXCLUSIVE, NULL)) != 0)
    385 		return (error);
    386 
    387 	switch (fmt) {
    388 	case S_IFCHR:
    389 		wd->sc_dk.dk_copenmask &= ~(1 << part);
    390 		break;
    391 	case S_IFBLK:
    392 		wd->sc_dk.dk_bopenmask &= ~(1 << part);
    393 		break;
    394 	}
    395 	wd->sc_dk.dk_openmask =
    396 	    wd->sc_dk.dk_copenmask | wd->sc_dk.dk_bopenmask;
    397 
    398 	if (wd->sc_dk.dk_openmask == 0) {
    399 #if 0
    400 		wd_flushcache(wd, AT_WAIT);
    401 #endif
    402 		/* XXXX Must wait for I/O to complete! */
    403 
    404 		if (! (wd->sc_flags & WDF_KLABEL))
    405 			wd->sc_flags &= ~WDF_LOADED;
    406 	}
    407 
    408 	(void) lockmgr(&wd->sc_dk.dk_openlock, LK_RELEASE, NULL);
    409 
    410 	return 0;
    411 }
    412 
    413 static void
    414 edgetdefaultlabel(ed, lp)
    415 	struct ed_softc *ed;
    416 	struct disklabel *lp;
    417 {
    418 	ATADEBUG_PRINT(("edgetdefaultlabel\n"), DEBUG_FUNCS);
    419 	memset(lp, 0, sizeof(struct disklabel));
    420 
    421 	lp->d_secsize = DEV_BSIZE;
    422 	lp->d_ntracks = ed->heads;
    423 	lp->d_nsectors = ed->sectors;
    424 	lp->d_ncylinders = ed->cyl;
    425 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
    426 
    427 	lp->d_type = DTYPE_ESDI;
    428 
    429 	strncpy(lp->d_typename, "ESDI", 16);
    430 	strncpy(lp->d_packname, "fictitious", 16);
    431 	lp->d_secperunit = ed->sc_capacity;
    432 	lp->d_rpm = 3600;
    433 	lp->d_interleave = 1;
    434 	lp->d_flags = 0;
    435 
    436 	lp->d_partitions[RAW_PART].p_offset = 0;
    437 	lp->d_partitions[RAW_PART].p_size =
    438 	lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
    439 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
    440 	lp->d_npartitions = RAW_PART + 1;
    441 
    442 	lp->d_magic = DISKMAGIC;
    443 	lp->d_magic2 = DISKMAGIC;
    444 	lp->d_checksum = dkcksum(lp);
    445 }
    446 
    447 /*
    448  * Fabricate a default disk label, and try to read the correct one.
    449  */
    450 static void
    451 edgetdisklabel(dev, ed)
    452 	dev_t dev;
    453 	struct ed_softc *ed;
    454 {
    455 	struct disklabel *lp = ed->sc_dk.dk_label;
    456 	const char *errstring;
    457 
    458 	ATADEBUG_PRINT(("edgetdisklabel\n"), DEBUG_FUNCS);
    459 
    460 	memset(ed->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
    461 
    462 	edgetdefaultlabel(ed, lp);
    463 
    464 	errstring = readdisklabel(
    465 	    EDLABELDEV(dev), edmcastrategy, lp, ed->sc_dk.dk_cpulabel);
    466 	if (errstring) {
    467 		/*
    468 		 * This probably happened because the drive's default
    469 		 * geometry doesn't match the DOS geometry.  We
    470 		 * assume the DOS geometry is now in the label and try
    471 		 * again.  XXX This is a kluge.
    472 		 */
    473 #if 0
    474 		if (wd->drvp->state > RECAL)
    475 			wd->drvp->drive_flags |= DRIVE_RESET;
    476 #endif
    477 		errstring = readdisklabel(EDLABELDEV(dev),
    478 			edmcastrategy, lp, ed->sc_dk.dk_cpulabel);
    479 	}
    480 	if (errstring) {
    481 		printf("%s: %s\n", ed->sc_dev.dv_xname, errstring);
    482 		return;
    483 	}
    484 }
    485 
    486 int
    487 edmcaioctl(dev, xfer, addr, flag, p)
    488 	dev_t dev;
    489 	u_long xfer;
    490 	caddr_t addr;
    491 	int flag;
    492 	struct proc *p;
    493 {
    494 	struct ed_softc *ed = device_lookup(&ed_cd, DISKUNIT(dev));
    495 	int error;
    496 
    497 	ATADEBUG_PRINT(("edioctl\n"), DEBUG_FUNCS);
    498 
    499 	if ((ed->sc_flags & WDF_LOADED) == 0)
    500 		return EIO;
    501 
    502 	switch (xfer) {
    503 	case DIOCGDINFO:
    504 		*(struct disklabel *)addr = *(ed->sc_dk.dk_label);
    505 		return 0;
    506 
    507 	case DIOCGPART:
    508 		((struct partinfo *)addr)->disklab = ed->sc_dk.dk_label;
    509 		((struct partinfo *)addr)->part =
    510 		    &ed->sc_dk.dk_label->d_partitions[DISKPART(dev)];
    511 		return 0;
    512 
    513 	case DIOCWDINFO:
    514 	case DIOCSDINFO:
    515 	{
    516 		struct disklabel *lp;
    517 
    518 		lp = (struct disklabel *)addr;
    519 
    520 		if ((flag & FWRITE) == 0)
    521 			return EBADF;
    522 
    523 		if ((error = lockmgr(&ed->sc_dk.dk_openlock, LK_EXCLUSIVE,
    524 				     NULL)) != 0)
    525 			return (error);
    526 		ed->sc_flags |= WDF_LABELLING;
    527 
    528 		error = setdisklabel(ed->sc_dk.dk_label,
    529 		    lp, /*wd->sc_dk.dk_openmask : */0,
    530 		    ed->sc_dk.dk_cpulabel);
    531 		if (error == 0) {
    532 #if 0
    533 			if (wd->drvp->state > RECAL)
    534 				wd->drvp->drive_flags |= DRIVE_RESET;
    535 #endif
    536 			if (xfer == DIOCWDINFO)
    537 				error = writedisklabel(EDLABELDEV(dev),
    538 				    edmcastrategy, ed->sc_dk.dk_label,
    539 				    ed->sc_dk.dk_cpulabel);
    540 		}
    541 
    542 		ed->sc_flags &= ~WDF_LABELLING;
    543 		(void) lockmgr(&ed->sc_dk.dk_openlock, LK_RELEASE, NULL);
    544 		return (error);
    545 	}
    546 
    547 	case DIOCKLABEL:
    548 		if (*(int *)addr)
    549 			ed->sc_flags |= WDF_KLABEL;
    550 		else
    551 			ed->sc_flags &= ~WDF_KLABEL;
    552 		return 0;
    553 
    554 	case DIOCWLABEL:
    555 		if ((flag & FWRITE) == 0)
    556 			return EBADF;
    557 		if (*(int *)addr)
    558 			ed->sc_flags |= WDF_WLABEL;
    559 		else
    560 			ed->sc_flags &= ~WDF_WLABEL;
    561 		return 0;
    562 
    563 	case DIOCGDEFLABEL:
    564 		edgetdefaultlabel(ed, (struct disklabel *)addr);
    565 		return 0;
    566 
    567 #if 0
    568 	case DIOCWFORMAT:
    569 		if ((flag & FWRITE) == 0)
    570 			return EBADF;
    571 		{
    572 		register struct format_op *fop;
    573 		struct iovec aiov;
    574 		struct uio auio;
    575 
    576 		fop = (struct format_op *)addr;
    577 		aiov.iov_base = fop->df_buf;
    578 		aiov.iov_len = fop->df_count;
    579 		auio.uio_iov = &aiov;
    580 		auio.uio_iovcnt = 1;
    581 		auio.uio_resid = fop->df_count;
    582 		auio.uio_segflg = 0;
    583 		auio.uio_offset =
    584 			fop->df_startblk * wd->sc_dk.dk_label->d_secsize;
    585 		auio.uio_procp = p;
    586 		error = physio(wdformat, NULL, dev, B_WRITE, minphys,
    587 		    &auio);
    588 		fop->df_count -= auio.uio_resid;
    589 		fop->df_reg[0] = wdc->sc_status;
    590 		fop->df_reg[1] = wdc->sc_error;
    591 		return error;
    592 		}
    593 #endif
    594 
    595 	case DIOCAWEDGE:
    596 	    {
    597 	    	struct dkwedge_info *dkw = (void *) addr;
    598 
    599 		if ((flag & FWRITE) == 0)
    600 			return (EBADF);
    601 
    602 		/* If the ioctl happens here, the parent is us. */
    603 		strcpy(dkw->dkw_parent, ed->sc_dev.dv_xname);
    604 		return (dkwedge_add(dkw));
    605 	    }
    606 
    607 	case DIOCDWEDGE:
    608 	    {
    609 	    	struct dkwedge_info *dkw = (void *) addr;
    610 
    611 		if ((flag & FWRITE) == 0)
    612 			return (EBADF);
    613 
    614 		/* If the ioctl happens here, the parent is us. */
    615 		strcpy(dkw->dkw_parent, ed->sc_dev.dv_xname);
    616 		return (dkwedge_del(dkw));
    617 	    }
    618 
    619 	case DIOCLWEDGES:
    620 	    {
    621 	    	struct dkwedge_list *dkwl = (void *) addr;
    622 
    623 		return (dkwedge_list(&ed->sc_dk, dkwl, p));
    624 	    }
    625 
    626 	default:
    627 		return ENOTTY;
    628 	}
    629 
    630 #ifdef DIAGNOSTIC
    631 	panic("edioctl: impossible");
    632 #endif
    633 }
    634 
    635 int
    636 edmcasize(dev)
    637 	dev_t dev;
    638 {
    639 	struct ed_softc *wd;
    640 	int part, omask;
    641 	int size;
    642 
    643 	ATADEBUG_PRINT(("edsize\n"), DEBUG_FUNCS);
    644 
    645 	wd = device_lookup(&ed_cd, DISKUNIT(dev));
    646 	if (wd == NULL)
    647 		return (-1);
    648 
    649 	part = DISKPART(dev);
    650 	omask = wd->sc_dk.dk_openmask & (1 << part);
    651 
    652 	if (omask == 0 && edmcaopen(dev, 0, S_IFBLK, NULL) != 0)
    653 		return (-1);
    654 	if (wd->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
    655 		size = -1;
    656 	else
    657 		size = wd->sc_dk.dk_label->d_partitions[part].p_size *
    658 		    (wd->sc_dk.dk_label->d_secsize / DEV_BSIZE);
    659 	if (omask == 0 && edmcaclose(dev, 0, S_IFBLK, NULL) != 0)
    660 		return (-1);
    661 	return (size);
    662 }
    663 
    664 /* #define WD_DUMP_NOT_TRUSTED if you just want to watch */
    665 static int eddoingadump = 0;
    666 static int eddumprecalibrated = 0;
    667 static int eddumpmulti = 1;
    668 
    669 /*
    670  * Dump core after a system crash.
    671  */
    672 int
    673 edmcadump(dev, blkno, va, size)
    674 	dev_t dev;
    675 	daddr_t blkno;
    676 	caddr_t va;
    677 	size_t size;
    678 {
    679 	struct ed_softc *ed;	/* disk unit to do the I/O */
    680 	struct disklabel *lp;   /* disk's disklabel */
    681 	int part;
    682 	int nblks;	/* total number of sectors left to write */
    683 	int error;
    684 
    685 	/* Check if recursive dump; if so, punt. */
    686 	if (eddoingadump)
    687 		return EFAULT;
    688 	eddoingadump = 1;
    689 
    690 	ed = device_lookup(&ed_cd, DISKUNIT(dev));
    691 	if (ed == NULL)
    692 		return (ENXIO);
    693 
    694 	part = DISKPART(dev);
    695 
    696 	/* Make sure it was initialized. */
    697 	if ((ed->sc_flags & EDF_INIT) == 0)
    698 		return ENXIO;
    699 
    700 	/* Convert to disk sectors.  Request must be a multiple of size. */
    701 	lp = ed->sc_dk.dk_label;
    702 	if ((size % lp->d_secsize) != 0)
    703 		return EFAULT;
    704 	nblks = size / lp->d_secsize;
    705 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
    706 
    707 	/* Check transfer bounds against partition size. */
    708 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
    709 		return EINVAL;
    710 
    711 	/* Offset block number to start of partition. */
    712 	blkno += lp->d_partitions[part].p_offset;
    713 
    714 	/* Recalibrate, if first dump transfer. */
    715 	if (eddumprecalibrated == 0) {
    716 		eddumprecalibrated = 1;
    717 		eddumpmulti = 8;
    718 #if 0
    719 		wd->drvp->state = RESET;
    720 #endif
    721 	}
    722 
    723 	while (nblks > 0) {
    724 		error = edc_bio(ed->edc_softc, ed, va, blkno,
    725 			min(nblks, eddumpmulti) * lp->d_secsize, 0, 1);
    726 		if (error)
    727 			return (error);
    728 
    729 		/* update block count */
    730 		nblks -= min(nblks, eddumpmulti);
    731 		blkno += min(nblks, eddumpmulti);
    732 		va += min(nblks, eddumpmulti) * lp->d_secsize;
    733 	}
    734 
    735 	eddoingadump = 0;
    736 	return (0);
    737 }
    738 
    739 static int
    740 ed_get_params(ed, drv_flags)
    741 	struct ed_softc *ed;
    742 	int *drv_flags;
    743 {
    744 	u_int16_t cmd_args[2];
    745 
    746 	/*
    747 	 * Get Device Configuration (09).
    748 	 */
    749 	cmd_args[0] = 14;	/* Options: 00s110, s: 0=Physical 1=Pseudo */
    750 	cmd_args[1] = 0;
    751 	if (edc_run_cmd(ed->edc_softc, CMD_GET_DEV_CONF, ed->sc_devno,
    752 	    cmd_args, 2, 1))
    753 		return (1);
    754 
    755 	ed->spares = ed->sense_data[1] >> 8;
    756 	if (drv_flags)
    757 		*drv_flags = ed->sense_data[1] & 0x1f;
    758 	ed->rba = ed->sense_data[2] | (ed->sense_data[3] << 16);
    759 	/* Instead of using:
    760 		ed->cyl = ed->sense_data[4];
    761 		ed->heads = ed->sense_data[5] & 0xff;
    762 		ed->sectors = ed->sense_data[5] >> 8;
    763 	 * we fabricate the numbers from RBA count, so that
    764 	 * number of sectors is 32 and heads 64. This seems
    765 	 * to be necessary for integrated ESDI controller.
    766 	 */
    767 	ed->sectors = 32;
    768 	ed->heads = 64;
    769 	ed->cyl = ed->rba / (ed->heads * ed->sectors);
    770 	ed->sc_capacity = ed->rba;
    771 
    772 	return (0);
    773 }
    774