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