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