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