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