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