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