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