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