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