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