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