Home | History | Annotate | Line # | Download | only in ebus
ace_ebus.c revision 1.2
      1  1.2  tsutsui /*	$NetBSD: ace_ebus.c,v 1.2 2011/06/18 17:47:20 tsutsui Exp $	*/
      2  1.1    pooka 
      3  1.1    pooka /*-
      4  1.1    pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1    pooka  * All rights reserved.
      6  1.1    pooka  *
      7  1.1    pooka  * This code was written by Alessandro Forin and Neil Pittman
      8  1.1    pooka  * at Microsoft Research and contributed to The NetBSD Foundation
      9  1.1    pooka  * by Microsoft Corporation.
     10  1.1    pooka  *
     11  1.1    pooka  * Redistribution and use in source and binary forms, with or without
     12  1.1    pooka  * modification, are permitted provided that the following conditions
     13  1.1    pooka  * are met:
     14  1.1    pooka  * 1. Redistributions of source code must retain the above copyright
     15  1.1    pooka  *    notice, this list of conditions and the following disclaimer.
     16  1.1    pooka  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1    pooka  *    notice, this list of conditions and the following disclaimer in the
     18  1.1    pooka  *    documentation and/or other materials provided with the distribution.
     19  1.1    pooka  *
     20  1.1    pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  1.1    pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  1.1    pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  1.1    pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  1.1    pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  1.1    pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  1.1    pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  1.1    pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  1.1    pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  1.1    pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  1.1    pooka  * POSSIBILITY OF SUCH DAMAGE.
     31  1.1    pooka  */
     32  1.1    pooka 
     33  1.1    pooka #include <sys/cdefs.h>
     34  1.2  tsutsui __KERNEL_RCSID(0, "$NetBSD: ace_ebus.c,v 1.2 2011/06/18 17:47:20 tsutsui Exp $");
     35  1.1    pooka 
     36  1.1    pooka #include "rnd.h"
     37  1.1    pooka 
     38  1.1    pooka #include <sys/param.h>
     39  1.1    pooka #include <sys/systm.h>
     40  1.1    pooka #include <sys/kernel.h>
     41  1.1    pooka #include <sys/conf.h>
     42  1.1    pooka #include <sys/file.h>
     43  1.1    pooka #include <sys/stat.h>
     44  1.1    pooka #include <sys/ioctl.h>
     45  1.1    pooka #include <sys/buf.h>
     46  1.1    pooka #include <sys/bufq.h>
     47  1.1    pooka #include <sys/uio.h>
     48  1.1    pooka #include <sys/malloc.h>
     49  1.1    pooka #include <sys/device.h>
     50  1.1    pooka #include <sys/disklabel.h>
     51  1.1    pooka #include <sys/disk.h>
     52  1.1    pooka #include <sys/syslog.h>
     53  1.1    pooka #include <sys/proc.h>
     54  1.1    pooka #include <sys/vnode.h>
     55  1.1    pooka #include <sys/kthread.h>
     56  1.1    pooka #include <sys/lock.h>
     57  1.1    pooka #include <sys/queue.h>
     58  1.1    pooka 
     59  1.1    pooka #if NRND > 0
     60  1.1    pooka #include <sys/rnd.h>
     61  1.1    pooka #endif
     62  1.1    pooka 
     63  1.1    pooka #include <machine/intr.h>
     64  1.1    pooka #include <machine/bus.h>
     65  1.1    pooka 
     66  1.1    pooka #include "locators.h"
     67  1.1    pooka #include <prop/proplib.h>
     68  1.1    pooka 
     69  1.1    pooka #include <emips/ebus/ebusvar.h>
     70  1.1    pooka #include <emips/emips/machdep.h>
     71  1.1    pooka #include <machine/emipsreg.h>
     72  1.1    pooka 
     73  1.1    pooka /* Structure returned by the Identify command (see CFlash specs)
     74  1.1    pooka  * NB: We only care for the first sector so that is what we define here.
     75  1.1    pooka  * NB: Beware of mis-alignment for all 32bit things
     76  1.1    pooka  */
     77  1.1    pooka typedef struct _CFLASH_IDENTIFY {
     78  1.2  tsutsui 	uint16_t Signature;				/* Word 0 */
     79  1.1    pooka #define CFLASH_SIGNATURE 0x848a
     80  1.2  tsutsui 	uint16_t DefaultNumberOfCylinders;		/* Word 1 */
     81  1.2  tsutsui 	uint16_t Reserved1;				/* Word 2 */
     82  1.2  tsutsui 	uint16_t DefaultNumberOfHeads;			/* Word 3 */
     83  1.2  tsutsui 	uint16_t Obsolete1[2];				/* Word 4 */
     84  1.2  tsutsui 	uint16_t DefaultSectorsPerTrack;		/* Word 6 */
     85  1.2  tsutsui 	uint16_t SectorsPerCard[2];			/* Word 7 */
     86  1.2  tsutsui 	uint16_t Obsolete2;				/* Word 9 */
     87  1.2  tsutsui 	uint8_t  SerialNumber[20]; /* padded, right-justified Word 10 */
     88  1.2  tsutsui 	uint16_t Obsolete3[2];				/* Word 20 */
     89  1.2  tsutsui 	uint16_t EccBytesInRWLong;			/* Word 22 */
     90  1.2  tsutsui 	uint8_t  FirmwareRevision[8];			/* Word 23 */
     91  1.2  tsutsui 	uint8_t  ModelNumber[40];			/* Word 27 */
     92  1.2  tsutsui 	uint16_t SectorsInRWMultiple;			/* Word 47 */
     93  1.2  tsutsui 	uint16_t Reserved2;				/* Word 48 */
     94  1.2  tsutsui 	uint16_t Capabilities;				/* Word 49 */
     95  1.2  tsutsui 	uint16_t Reserved3;				/* Word 50 */
     96  1.2  tsutsui 	uint16_t PioMode;				/* Word 51 */
     97  1.2  tsutsui 	uint16_t Obsolete4;				/* Word 52 */
     98  1.2  tsutsui 	uint16_t FieldValidity;				/* Word 53 */
     99  1.2  tsutsui 	uint16_t CurrentNumberOfCylinders;		/* Word 54 */
    100  1.2  tsutsui 	uint16_t CurrentNumberOfHeads;			/* Word 55 */
    101  1.2  tsutsui 	uint16_t CurrentSectorsPerTrack;		/* Word 56 */
    102  1.2  tsutsui 	uint16_t CurrentCapacity[2];			/* Word 57 */
    103  1.2  tsutsui 	uint16_t MultiSectorSettings;			/* Word 59 */
    104  1.2  tsutsui 	uint16_t NumberOfAddressableSectors[2];		/* Word 60 */
    105  1.2  tsutsui 	uint16_t Reserved4;				/* Word 62 */
    106  1.2  tsutsui 	uint16_t MultiWordDmaTransfer;			/* Word 63 */
    107  1.2  tsutsui 	uint16_t AdvancedPioModes;			/* Word 64 */
    108  1.2  tsutsui 	uint16_t MinimumMultiWordDmaTiming;		/* Word 65 */
    109  1.2  tsutsui 	uint16_t RecommendedMultiWordDmaTiming;		/* Word 66 */
    110  1.2  tsutsui 	uint16_t PioTimingNoFlowControl;		/* Word 67 */
    111  1.2  tsutsui 	uint16_t PioTimingWithFlowControl;		/* Word 68 */
    112  1.2  tsutsui 	uint16_t Reserved5[13];				/* Word 69 */
    113  1.2  tsutsui 	uint16_t FeaturesSupported[3];			/* Word 82 */
    114  1.2  tsutsui 	uint16_t FeaturesEnabled[3];			/* Word 85 */
    115  1.2  tsutsui 	uint16_t UdmaMode;				/* Word 88 */
    116  1.2  tsutsui 	uint16_t SecurityEraseTime;			/* Word 89 */
    117  1.2  tsutsui 	uint16_t EnhancedSecurityEraseTime;		/* Word 90 */
    118  1.2  tsutsui 	uint16_t CurrentPowerManagementValue;		/* Word 91 */
    119  1.2  tsutsui 	uint8_t  Reserved6[72];				/* Word 92-127 */
    120  1.2  tsutsui 	uint8_t  SecondHalf[256];			/* Word 128-255 */
    121  1.1    pooka } CFLASH_IDENTIFY, *PCFLASH_IDENTIFY;
    122  1.1    pooka 
    123  1.1    pooka #define SIZEOF_IDENTIFY CF_SECTOR_SIZE /* must be a sector multiple */
    124  1.1    pooka 
    125  1.1    pooka /* Instead of dragging in atavar.h.. */
    126  1.1    pooka /*
    127  1.1    pooka  * Parameters/state needed by the controller to perform an ATA bio.
    128  1.1    pooka  */
    129  1.1    pooka struct ace_bio {
    130  1.1    pooka 	volatile int flags;/* cmd flags */
    131  1.1    pooka #define	ATA_POLL	0x0002	/* poll for completion */
    132  1.1    pooka #define	ATA_SINGLE	0x0008	/* transfer must be done in singlesector mode */
    133  1.1    pooka #define	ATA_READ	0x0020	/* transfer is a read (otherwise a write) */
    134  1.1    pooka #define	ATA_CORR	0x0040	/* transfer had a corrected error */
    135  1.1    pooka 	daddr_t		blkno;	/* block addr */
    136  1.1    pooka 	daddr_t		blkdone;/* number of blks transferred */
    137  1.1    pooka 	size_t		nblks;	/* number of blocks currently transferring */
    138  1.2  tsutsui 	size_t		nbytes;	/* number of bytes currently transferring */
    139  1.1    pooka 	char		*databuf;/* data buffer address */
    140  1.1    pooka 	volatile int	error;
    141  1.2  tsutsui #define	NOERROR		0	/* There was no error (r_error invalid),
    142  1.2  tsutsui 				   else see acedone()*/
    143  1.1    pooka #define FAILED(er) (er != 0)
    144  1.1    pooka #define EDOOFUS EIO
    145  1.1    pooka 
    146  1.2  tsutsui 	uint32_t	r_error;/* copy of status register */
    147  1.1    pooka #ifdef HAS_BAD144_HANDLING
    148  1.1    pooka 	daddr_t		badsect[127];/* 126 plus trailing -1 marker */
    149  1.1    pooka #endif
    150  1.1    pooka };
    151  1.1    pooka /* End of atavar.h*/
    152  1.1    pooka 
    153  1.1    pooka struct ace_softc {
    154  1.1    pooka 	/* General disk infos */
    155  1.1    pooka 	device_t sc_dev;
    156  1.1    pooka 
    157  1.1    pooka 	struct disk sc_dk;
    158  1.1    pooka 	struct bufq_state *sc_q;
    159  1.1    pooka 	struct callout sc_restart_ch;
    160  1.1    pooka 
    161  1.1    pooka 	/* IDE disk soft states */
    162  1.1    pooka 	struct buf *sc_bp; /* buf being transfered */
    163  1.1    pooka 	struct buf *active_xfer; /* buf handoff to thread  */
    164  1.2  tsutsui 	/* current transfer data */
    165  1.1    pooka 	struct ace_bio sc_bio; /* current transfer */
    166  1.1    pooka 
    167  1.2  tsutsui 	struct proc *ch_thread;
    168  1.2  tsutsui 	int ch_flags;
    169  1.1    pooka #define ATACH_SHUTDOWN 0x02        /* thread is shutting down */
    170  1.1    pooka #define ATACH_IRQ_WAIT 0x10        /* thread is waiting for irq */
    171  1.1    pooka #define ATACH_DISABLED 0x80        /* channel is disabled */
    172  1.1    pooka #define ATACH_TH_RUN   0x100       /* the kernel thread is working */
    173  1.1    pooka #define ATACH_TH_RESET 0x200       /* someone ask the thread to reset */
    174  1.1    pooka 
    175  1.1    pooka 	int openings;
    176  1.2  tsutsui 	int media_has_changed;
    177  1.1    pooka #define    ACECE_MC    0x20    /* media changed */
    178  1.1    pooka #define    ACECE_MCR   0x08    /* media change requested */
    179  1.1    pooka 	struct _CFLASH_IDENTIFY sc_params;/* drive characteristics found */
    180  1.1    pooka 
    181  1.1    pooka 	int sc_flags;
    182  1.1    pooka #define	ACEF_WLABEL	0x004 /* label is writable */
    183  1.1    pooka #define	ACEF_LABELLING	0x008 /* writing label */
    184  1.1    pooka #define ACEF_LOADED	0x010 /* parameters loaded */
    185  1.1    pooka #define ACEF_WAIT	0x020 /* waiting for resources */
    186  1.1    pooka #define ACEF_KLABEL	0x080 /* retain label after 'full' close */
    187  1.1    pooka 
    188  1.2  tsutsui 	uint64_t sc_capacity;
    189  1.1    pooka 	uint32_t sc_multi; /* max sectors per xfer */
    190  1.1    pooka 
    191  1.1    pooka 	struct	_Sac   *sc_dr;		/* reg pointers */
    192  1.2  tsutsui 	int hw_busy;
    193  1.1    pooka 	int retries; /* number of xfer retry */
    194  1.1    pooka 
    195  1.1    pooka #if NRND > 0
    196  1.1    pooka 	rndsource_element_t	rnd_source;
    197  1.1    pooka #endif
    198  1.1    pooka };
    199  1.1    pooka 
    200  1.2  tsutsui int  ace_ebus_match(device_t, cfdata_t, void *);
    201  1.2  tsutsui void ace_ebus_attach(device_t, device_t, void *);
    202  1.1    pooka void aceattach(struct ace_softc *);
    203  1.2  tsutsui int	 acedetach(device_t, int);
    204  1.2  tsutsui int	 aceactivate(device_t, enum devact);
    205  1.1    pooka 
    206  1.1    pooka void  acedone(struct ace_softc *);
    207  1.1    pooka static void ace_params_to_properties(struct ace_softc *ace);
    208  1.1    pooka 
    209  1.1    pooka CFATTACH_DECL_NEW(ace_ebus, sizeof(struct ace_softc),
    210  1.1    pooka     ace_ebus_match, ace_ebus_attach, acedetach, aceactivate);
    211  1.1    pooka 
    212  1.1    pooka int  ace_ebus_intr(void *cookie, void *f);
    213  1.1    pooka 
    214  1.1    pooka static void sysace_thread(void *arg);
    215  1.1    pooka 
    216  1.1    pooka int
    217  1.2  tsutsui ace_ebus_match(device_t parent, cfdata_t cf, void *aux)
    218  1.1    pooka {
    219  1.1    pooka 	struct ebus_attach_args *d = aux;
    220  1.2  tsutsui 	struct _Sac *sac = (struct _Sac *)d->ia_vaddr;
    221  1.1    pooka 
    222  1.2  tsutsui 	if (strcmp("ace", d->ia_name) != 0)
    223  1.2  tsutsui 		return 0;
    224  1.2  tsutsui 	if ((sac == NULL) ||
    225  1.2  tsutsui 	    ((sac->Tag & SAC_TAG) != PMTTAG_SYSTEM_ACE))
    226  1.2  tsutsui 		return 0;
    227  1.2  tsutsui 	return 1;
    228  1.1    pooka }
    229  1.1    pooka 
    230  1.1    pooka void
    231  1.2  tsutsui ace_ebus_attach(device_t parent, device_t self, void *aux)
    232  1.1    pooka {
    233  1.1    pooka 	struct ace_softc *ace = device_private(self);
    234  1.1    pooka 	struct ebus_attach_args *ia = aux;
    235  1.1    pooka 	int error;
    236  1.1    pooka 
    237  1.1    pooka 	/*
    238  1.1    pooka 	 * It's on the baseboard, with a dedicated interrupt line.
    239  1.1    pooka 	 */
    240  1.1    pooka 	ace->sc_dr = (struct _Sac *)ia->ia_vaddr;
    241  1.1    pooka 	ace->sc_dev = self;
    242  1.1    pooka #if DEBUG
    243  1.1    pooka 	printf(" virt=%p", (void*)ace->sc_dr);
    244  1.1    pooka #endif
    245  1.1    pooka 	printf(" : System ACE\n");
    246  1.1    pooka 
    247  1.1    pooka 	ebus_intr_establish(parent, (void*)ia->ia_cookie, IPL_BIO,
    248  1.1    pooka 	    ace_ebus_intr, ace);
    249  1.1    pooka 
    250  1.1    pooka 	config_pending_incr();
    251  1.1    pooka 
    252  1.1    pooka 	error = kthread_create(PRI_NONE, 0, NULL, sysace_thread,
    253  1.1    pooka 	    ace, NULL, "%s", device_xname(ace->sc_dev));
    254  1.1    pooka 	if (error)
    255  1.1    pooka 		aprint_error_dev(ace->sc_dev, "unable to create kernel "
    256  1.1    pooka 		    "thread: error %d\n", error);
    257  1.1    pooka }
    258  1.1    pooka 
    259  1.2  tsutsui /*
    260  1.2  tsutsui  * Sysace driver I(af) wrote for FreeBsd.
    261  1.1    pooka  */
    262  1.1    pooka #define CF_SECBITS     9
    263  1.1    pooka #define CF_SECTOR_SIZE (1 << CF_SECBITS)
    264  1.1    pooka 
    265  1.1    pooka static int sysace_attach(struct ace_softc *sc);
    266  1.1    pooka static int sysace_reset(struct ace_softc *sc);
    267  1.1    pooka static int sysace_identify(struct ace_softc *sc);
    268  1.1    pooka static int sysace_lock_registers(struct ace_softc *sc);
    269  1.1    pooka static int sysace_unlock_registers(struct ace_softc *sc);
    270  1.2  tsutsui static int sysace_start(struct ace_softc *sc, uint32_t Command, uint32_t Lba,
    271  1.2  tsutsui 			uint32_t nSectors);
    272  1.1    pooka static int sysace_validate(struct ace_softc *sc, daddr_t start, size_t *pSize);
    273  1.2  tsutsui static int sysace_read_at (struct ace_softc *sc, daddr_t start_sector,
    274  1.2  tsutsui 			   char *buffer, size_t nblocks, size_t * pSizeRead);
    275  1.2  tsutsui static int sysace_write_at(struct ace_softc *sc, daddr_t start_sector,
    276  1.2  tsutsui 			   char *buffer, size_t nblocks, size_t * pSizeWritten);
    277  1.1    pooka #ifdef USE_ACE_FOR_RECONFIG /* Old code, despised and replaced by ICAP */
    278  1.1    pooka static int sysace_send_config(struct ace_softc *sc,
    279  1.1    pooka                               uint32_t *Data, unsigned int nBytes);
    280  1.1    pooka #endif
    281  1.1    pooka 
    282  1.1    pooka #define DEBUG_INTR   0x01
    283  1.1    pooka #define DEBUG_XFERS  0x02
    284  1.1    pooka #define DEBUG_STATUS 0x04
    285  1.1    pooka #define DEBUG_FUNCS  0x08
    286  1.1    pooka #define DEBUG_PROBE  0x10
    287  1.1    pooka #define DEBUG_WRITES 0x20
    288  1.1    pooka #define DEBUG_READS  0x40
    289  1.1    pooka #define DEBUG_ERRORS 0x80
    290  1.1    pooka #ifdef DEBUG
    291  1.2  tsutsui int ace_debug = DEBUG_ERRORS /*|DEBUG_WRITES*/;
    292  1.1    pooka #define ACE_DEBUG(x) (ace_debug & (x))
    293  1.1    pooka #define DBGME(_lev_,_x_) if ((_lev_) & ace_debug) _x_
    294  1.1    pooka #else
    295  1.1    pooka #define ACE_DEBUG(x) (0)
    296  1.1    pooka #define DBGME(_lev_,_x_)
    297  1.1    pooka #endif
    298  1.1    pooka #define DEBUG_PRINT(_args_,_lev_) DBGME(_lev_,printf _args_)
    299  1.1    pooka 
    300  1.1    pooka static int
    301  1.1    pooka sysace_attach(struct ace_softc *sc)
    302  1.1    pooka {
    303  1.2  tsutsui 	int error;
    304  1.2  tsutsui 
    305  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("Sysace::delayed_attach %p\n", sc));
    306  1.2  tsutsui 
    307  1.2  tsutsui 	sc->media_has_changed = TRUE;
    308  1.2  tsutsui 	sc->sc_capacity = 0;
    309  1.2  tsutsui 
    310  1.2  tsutsui 	error = sysace_reset(sc);
    311  1.2  tsutsui 	if (error) {
    312  1.2  tsutsui 		device_printf(sc->sc_dev,
    313  1.2  tsutsui 		    "failed to reset, errno=%d\n", error);
    314  1.2  tsutsui 		goto Out;
    315  1.2  tsutsui 	}
    316  1.2  tsutsui 
    317  1.2  tsutsui 	error = sysace_identify(sc);
    318  1.2  tsutsui 	if (error) {
    319  1.2  tsutsui 		device_printf(sc->sc_dev,
    320  1.2  tsutsui 		    "failed to identify card, errno=%d.\n", error);
    321  1.2  tsutsui 		goto Out;
    322  1.2  tsutsui 	}
    323  1.1    pooka 
    324  1.2  tsutsui 	DBGME(DEBUG_PROBE, device_printf(sc->sc_dev,
    325  1.2  tsutsui 	    "Card has %qx sectors.\n", sc->sc_capacity));
    326  1.1    pooka 	if (sc->sc_capacity == 0) {
    327  1.2  tsutsui 		device_printf(sc->sc_dev, "size 0, no card? Wont work.\n");
    328  1.2  tsutsui 		error = EDOOFUS;
    329  1.2  tsutsui 		goto Out;
    330  1.2  tsutsui 	}
    331  1.1    pooka 
    332  1.2  tsutsui 	sc->media_has_changed = FALSE;
    333  1.1    pooka Out:
    334  1.2  tsutsui 	return error;
    335  1.1    pooka }
    336  1.1    pooka 
    337  1.1    pooka static void
    338  1.1    pooka sysace_wedges(void *arg);
    339  1.1    pooka extern int	dkwedge_autodiscover;
    340  1.1    pooka 
    341  1.2  tsutsui /*
    342  1.2  tsutsui  * Aux temp thread to avoid deadlock when doing
    343  1.2  tsutsui  * the partitio.. ahem wedges thing.
    344  1.1    pooka  */
    345  1.1    pooka static void
    346  1.1    pooka sysace_wedges(void *arg)
    347  1.1    pooka {
    348  1.2  tsutsui 	struct ace_softc *sc = arg;
    349  1.1    pooka 
    350  1.2  tsutsui 	DBGME(DEBUG_STATUS, printf("Sysace::wedges started for %p\n", sc));
    351  1.1    pooka 
    352  1.1    pooka 	/* Discover wedges on this disk. */
    353  1.1    pooka 	dkwedge_autodiscover = 1;
    354  1.1    pooka 	dkwedge_discover(&sc->sc_dk);
    355  1.1    pooka 
    356  1.1    pooka 	config_pending_decr();
    357  1.1    pooka 
    358  1.2  tsutsui 	DBGME(DEBUG_STATUS, printf("Sysace::thread done for %p\n", sc));
    359  1.2  tsutsui 	kthread_exit(0);
    360  1.1    pooka }
    361  1.1    pooka 
    362  1.1    pooka static void
    363  1.1    pooka sysace_thread(void *arg)
    364  1.1    pooka {
    365  1.2  tsutsui 	struct ace_softc *sc = arg;
    366  1.1    pooka 	struct buf *bp;
    367  1.1    pooka 	int s, error;
    368  1.1    pooka 
    369  1.2  tsutsui 	DBGME(DEBUG_STATUS, printf("Sysace::thread started for %p\n", sc));
    370  1.1    pooka 
    371  1.1    pooka 	s = splbio();
    372  1.1    pooka 	aceattach(sc);
    373  1.1    pooka 	splx(s);
    374  1.1    pooka 
    375  1.1    pooka 	error = kthread_create(PRI_NONE, 0 /* MPSAFE??? */, NULL,
    376  1.1    pooka 	    sysace_wedges, sc, NULL, "%s.wedges", device_xname(sc->sc_dev));
    377  1.1    pooka 	if (error)
    378  1.1    pooka 		aprint_error_dev(sc->sc_dev, "wedges: unable to create "
    379  1.1    pooka 		    "kernel thread: error %d\n", error);
    380  1.1    pooka 
    381  1.2  tsutsui 	DBGME(DEBUG_STATUS,
    382  1.2  tsutsui 	    printf("Sysace::thread service active for %p\n", sc));
    383  1.1    pooka 
    384  1.2  tsutsui 	s = splbio();
    385  1.1    pooka 	for (;;) {
    386  1.2  tsutsui 		/* Get next I/O request, wait if necessary */
    387  1.1    pooka 		if ((sc->ch_flags & (ATACH_TH_RESET | ATACH_SHUTDOWN)) == 0 &&
    388  1.1    pooka 		    (sc->active_xfer == NULL)) {
    389  1.1    pooka 			sc->ch_flags &= ~ATACH_TH_RUN;
    390  1.1    pooka 			(void) tsleep(&sc->ch_thread, PRIBIO, "aceth", 0);
    391  1.1    pooka 			sc->ch_flags |= ATACH_TH_RUN;
    392  1.1    pooka 		}
    393  1.2  tsutsui 		if (sc->ch_flags & ATACH_SHUTDOWN)
    394  1.1    pooka 			break;
    395  1.2  tsutsui 		bp = sc->active_xfer;
    396  1.2  tsutsui 		sc->active_xfer = NULL;
    397  1.1    pooka 		if (bp != NULL) {
    398  1.2  tsutsui 			size_t sz, bnow;
    399  1.1    pooka 
    400  1.2  tsutsui 			DBGME(DEBUG_XFERS,
    401  1.2  tsutsui 			    printf("Sysace::task %p %p %x %p %qx %d (%zd)\n",
    402  1.2  tsutsui 			    sc, bp, sc->sc_bio.flags, sc->sc_bio.databuf,
    403  1.2  tsutsui 			    sc->sc_bio.blkno, sc->sc_bio.nbytes,
    404  1.2  tsutsui 			    sc->sc_bio.nblks));
    405  1.2  tsutsui 
    406  1.2  tsutsui 			sc->sc_bio.error = 0;
    407  1.2  tsutsui 			for (; sc->sc_bio.nblks > 0;) {
    408  1.2  tsutsui 
    409  1.2  tsutsui 				bnow = sc->sc_bio.nblks;
    410  1.2  tsutsui 				if (sc->sc_bio.flags & ATA_SINGLE)
    411  1.2  tsutsui 					bnow = 1;
    412  1.2  tsutsui 
    413  1.2  tsutsui 				if (sc->sc_bio.flags & ATA_READ) {
    414  1.2  tsutsui 					sc->sc_bio.error =
    415  1.2  tsutsui 					    sysace_read_at(sc,
    416  1.2  tsutsui 					    sc->sc_bio.blkno,
    417  1.2  tsutsui 					    sc->sc_bio.databuf, bnow, &sz);
    418  1.2  tsutsui 				} else {
    419  1.2  tsutsui 					sc->sc_bio.error =
    420  1.2  tsutsui 					    sysace_write_at(sc,
    421  1.2  tsutsui 					    sc->sc_bio.blkno,
    422  1.2  tsutsui 					    sc->sc_bio.databuf, bnow, &sz);
    423  1.2  tsutsui 				}
    424  1.2  tsutsui 
    425  1.2  tsutsui 				if (FAILED(sc->sc_bio.error))
    426  1.2  tsutsui 					break;
    427  1.2  tsutsui 
    428  1.2  tsutsui 				sc->sc_bio.blkno += sz; /* in blocks */
    429  1.2  tsutsui 				sc->sc_bio.nblks -= sz;
    430  1.2  tsutsui 				sc->sc_bio.blkdone += sz;
    431  1.2  tsutsui 				sz = sz << CF_SECBITS; /* in bytes */
    432  1.2  tsutsui 				sc->sc_bio.databuf += sz;
    433  1.2  tsutsui 				sc->sc_bio.nbytes  -= sz;
    434  1.2  tsutsui 			}
    435  1.1    pooka 
    436  1.2  tsutsui 			acedone(sc);
    437  1.2  tsutsui 		}
    438  1.1    pooka 	}
    439  1.1    pooka 
    440  1.1    pooka 	splx(s);
    441  1.1    pooka 	sc->ch_thread = NULL;
    442  1.1    pooka 	wakeup(&sc->ch_flags);
    443  1.1    pooka 	kthread_exit(0);
    444  1.1    pooka }
    445  1.1    pooka 
    446  1.1    pooka /* Worker routines
    447  1.1    pooka  */
    448  1.1    pooka #if _DEBUG
    449  1.1    pooka typedef char *NAME;
    450  1.1    pooka typedef struct _REGDESC {
    451  1.2  tsutsui 	NAME RegisterName;
    452  1.2  tsutsui 	NAME BitNames[32];
    453  1.1    pooka } REGDESC, *PREGDESC;
    454  1.1    pooka 
    455  1.2  tsutsui static void
    456  1.2  tsutsui SysacePrintRegister(const REGDESC *Desc, uint32_t Value)
    457  1.1    pooka {
    458  1.2  tsutsui 	int i;
    459  1.2  tsutsui 
    460  1.2  tsutsui 	printf("\t%s %x =", Desc->RegisterName, Value);
    461  1.2  tsutsui 	for (i = 31; i >= 0; i--) {
    462  1.2  tsutsui 		if (Value & (1 << i))
    463  1.2  tsutsui 			printf(" %s",
    464  1.2  tsutsui 			    (Desc->BitNames[i]) ? Desc->BitNames[i] : "?");
    465  1.2  tsutsui 	}
    466  1.2  tsutsui 	printf("\n");
    467  1.2  tsutsui }
    468  1.2  tsutsui 
    469  1.2  tsutsui static uint32_t
    470  1.2  tsutsui SysaceDumpRegisters(struct _Sac *regs)
    471  1.2  tsutsui {
    472  1.2  tsutsui 	const REGDESC Control_Names = {
    473  1.2  tsutsui 		"Control",
    474  1.2  tsutsui 		{
    475  1.2  tsutsui 			0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
    476  1.2  tsutsui 			"RST",		/* 0x00010000 */
    477  1.2  tsutsui 			"BUS8",		/* 0x00020000 */
    478  1.2  tsutsui 			"BUS16",	/* 0x00040000 */
    479  1.2  tsutsui 			"BUS32",	/* 0x00080000 */
    480  1.2  tsutsui 			"IRQ",		/* 0x00100000 */
    481  1.2  tsutsui 			"BRDY",		/* 0x00200000 */
    482  1.2  tsutsui 			"IMSK0",	/* 0x00400000 */
    483  1.2  tsutsui 			"IMSK1",	/* 0x00800000 */
    484  1.2  tsutsui 			"TD0",		/* 0x0f000000 */
    485  1.2  tsutsui 			"TD1",		/* 0x0f000000 */
    486  1.2  tsutsui 			"TD2",		/* 0x0f000000 */
    487  1.2  tsutsui 			"TD3",		/* 0x0f000000 */
    488  1.2  tsutsui 			"BUFW8",	/* 0x10000000 */
    489  1.2  tsutsui 			"BUFW16",	/* 0x20000000 */
    490  1.2  tsutsui 			"BUFW32",	/* 0x40000000 */
    491  1.2  tsutsui 			"DEBUG"		/* 0x80000000 */
    492  1.2  tsutsui 		}
    493  1.2  tsutsui 	};
    494  1.2  tsutsui 
    495  1.2  tsutsui 	const REGDESC STATUS_Names = {
    496  1.2  tsutsui 		"STATUS",
    497  1.2  tsutsui 		{
    498  1.2  tsutsui 			"CFGLOCK",	/* 0x00000001 */
    499  1.2  tsutsui 			"MPULOCK",	/* 0x00000002 */
    500  1.2  tsutsui 			"CFGERROR",	/* 0x00000004 */
    501  1.2  tsutsui 			"CFCERROR",	/* 0x00000008 */
    502  1.2  tsutsui 			"CFDETECT",	/* 0x00000010 */
    503  1.2  tsutsui 			"DATABUFRDY",	/* 0x00000020 */
    504  1.2  tsutsui 			"DATABUFWRITE",	/* 0x00000040 */
    505  1.2  tsutsui 			"CFGDONE",	/* 0x00000080 */
    506  1.2  tsutsui 			"RDYFORCFCMD",	/* 0x00000100 */
    507  1.2  tsutsui 			"CFGMODEPIN",	/* 0x00000200 */
    508  1.2  tsutsui 			0,0,0,
    509  1.2  tsutsui 			"CFGADDRPIN0",	/* 0x0000e000 */
    510  1.2  tsutsui 			"CFGADDRPIN1",	/* 0x0000e000 */
    511  1.2  tsutsui 			"CFGADDRPIN2",	/* 0x0000e000 */
    512  1.2  tsutsui 			0,
    513  1.2  tsutsui 			"CFBSY",	/* 0x00020000 */
    514  1.2  tsutsui 			"CFRDY",	/* 0x00040000 */
    515  1.2  tsutsui 			"CFDWF",	/* 0x00080000 */
    516  1.2  tsutsui 			"CFDSC",	/* 0x00100000 */
    517  1.2  tsutsui 			"CFDRQ",	/* 0x00200000 */
    518  1.2  tsutsui 			"CFCORR",	/* 0x00400000 */
    519  1.2  tsutsui 			"CFERR",	/* 0x00800000 */
    520  1.2  tsutsui 			0,
    521  1.2  tsutsui 		}
    522  1.2  tsutsui 	};
    523  1.2  tsutsui 
    524  1.2  tsutsui 	const REGDESC ERRORREG_Names = {
    525  1.2  tsutsui 		"ERRORREG",
    526  1.2  tsutsui 		{
    527  1.2  tsutsui 			"CARDRESETERR",	/* 0x00000001 */
    528  1.2  tsutsui 			"CARDRDYERR",	/* 0x00000002 */
    529  1.2  tsutsui 			"CARDREADERR",	/* 0x00000004 */
    530  1.2  tsutsui 			"CARDWRITEERR",	/* 0x00000008 */
    531  1.2  tsutsui 			"SECTORRDYERR",	/* 0x00000010 */
    532  1.2  tsutsui 			"CFGADDRERR",	/* 0x00000020 */
    533  1.2  tsutsui 			"CFGFAILED",	/* 0x00000040 */
    534  1.2  tsutsui 			"CFGREADERR",	/* 0x00000080 */
    535  1.2  tsutsui 			"CFGINSTRERR",	/* 0x00000100 */
    536  1.2  tsutsui 			"CFGINITERR",	/* 0x00000200 */
    537  1.2  tsutsui 			0,
    538  1.2  tsutsui 			"CFBBK",	/* 0x00000800 */
    539  1.2  tsutsui 			"CFUNC",	/* 0x00001000 */
    540  1.2  tsutsui 			"CFIDNF",	/* 0x00002000 */
    541  1.2  tsutsui 			"CFABORT",	/* 0x00004000 */
    542  1.2  tsutsui 			"CFAMNF",	/* 0x00008000 */
    543  1.2  tsutsui 			0,
    544  1.2  tsutsui 		}
    545  1.2  tsutsui 	};
    546  1.2  tsutsui 
    547  1.2  tsutsui 	const NAME CommandNames[8] = {
    548  1.2  tsutsui 		"0",			/* 0x0000 */
    549  1.2  tsutsui 		"RESETMEMCARD",		/* 0x0100 */
    550  1.2  tsutsui 		"IDENTIFYMEMCARD",	/* 0x0200 */
    551  1.2  tsutsui 		"READMEMCARDDATA",	/* 0x0300 */
    552  1.2  tsutsui 		"WRITEMEMCARDDATA",	/* 0x0400 */
    553  1.2  tsutsui 		"5",			/* 0x0500 */
    554  1.2  tsutsui 		"ABORT",		/* 0x0600 */
    555  1.2  tsutsui 		"7"			/* 0x0700 */
    556  1.2  tsutsui 	};
    557  1.2  tsutsui 
    558  1.2  tsutsui 	const REGDESC CONTROLREG_Names = {
    559  1.2  tsutsui 		"CONTROLREG",
    560  1.2  tsutsui 		{
    561  1.2  tsutsui 			"FORCELOCKREQ",	/* 0x00000001 */
    562  1.2  tsutsui 			"LOCKREQ",	/* 0x00000002 */
    563  1.2  tsutsui 			"FORCECFGADDR",	/* 0x00000004 */
    564  1.2  tsutsui 			"FORCECFGMODE",	/* 0x00000008 */
    565  1.2  tsutsui 			"CFGMODE",	/* 0x00000010 */
    566  1.2  tsutsui 			"CFGSTART",	/* 0x00000020 */
    567  1.2  tsutsui 			"CFGSEL_MPU",	/* 0x00000040 */
    568  1.2  tsutsui 			"CFGRESET",	/* 0x00000080 */
    569  1.2  tsutsui 			"DATABUFRDYIRQ",/* 0x00000100 */
    570  1.2  tsutsui 			"ERRORIRQ",	/* 0x00000200 */
    571  1.2  tsutsui 			"CFGDONEIRQ",	/* 0x00000400 */
    572  1.2  tsutsui 			"RESETIRQ",	/* 0x00000800 */
    573  1.2  tsutsui 			"CFGPROG",	/* 0x00001000 */
    574  1.2  tsutsui 			"CFGADDR_B0",	/* 0x00002000 */
    575  1.2  tsutsui 			"CFGADDR_B1",	/* 0x00004000 */
    576  1.2  tsutsui 			"CFGADDR_B2",	/* 0x00008000 */
    577  1.2  tsutsui 			0,
    578  1.2  tsutsui 		}
    579  1.2  tsutsui 	};
    580  1.1    pooka 
    581  1.2  tsutsui 	const REGDESC FATSTATREG_Names = {
    582  1.2  tsutsui 		"FATSTATREG",
    583  1.2  tsutsui 		{
    584  1.2  tsutsui 			"MBRVALID",	/* 0x00000001 */
    585  1.2  tsutsui 			"PBRVALID",	/* 0x00000002 */
    586  1.2  tsutsui 			"MBRFAT12",	/* 0x00000004 */
    587  1.2  tsutsui 			"PBRFAT12",	/* 0x00000008 */
    588  1.2  tsutsui 			"MBRFAT16",	/* 0x00000010 */
    589  1.2  tsutsui 			"PBRFAT16",	/* 0x00000020 */
    590  1.2  tsutsui 			"CALCFAT12",	/* 0x00000040 */
    591  1.2  tsutsui 			"CALCFAT16",	/* 0x00000080 */
    592  1.2  tsutsui 			0,
    593  1.2  tsutsui 		}
    594  1.2  tsutsui 	};
    595  1.2  tsutsui 
    596  1.2  tsutsui 	printf("Sysace@%p:\n", regs);
    597  1.2  tsutsui 	printf("\tTag %x\n", regs->Tag);
    598  1.2  tsutsui 	SysacePrintRegister(&Control_Names, regs->Control);
    599  1.2  tsutsui 	printf("\tBUSMODEREG %x\n", regs->BUSMODEREG);
    600  1.2  tsutsui 	SysacePrintRegister(&STATUS_Names, regs->STATUS);
    601  1.2  tsutsui 	SysacePrintRegister(&ERRORREG_Names, regs->ERRORREG);
    602  1.2  tsutsui 	printf("\tCFGLBAREG %x\n", regs->CFGLBAREG);
    603  1.2  tsutsui 	printf("\tMPULBAREG %x\n", regs->MPULBAREG);
    604  1.2  tsutsui 	printf("\tVERSIONREG %x\n", regs->VERSIONREG);
    605  1.2  tsutsui 	printf("\tSECCNTCMDREG %x = %s cnt=%d\n", regs->SECCNTCMDREG,
    606  1.2  tsutsui 	    CommandNames[(regs->SECCNTCMDREG >> 8) & 7],
    607  1.2  tsutsui 	    regs->SECCNTCMDREG & SAC_SECCCNT);
    608  1.2  tsutsui 	SysacePrintRegister(&CONTROLREG_Names, regs->CONTROLREG);
    609  1.2  tsutsui 	SysacePrintRegister(&FATSTATREG_Names, regs->FATSTATREG);
    610  1.2  tsutsui 
    611  1.2  tsutsui 	return 1;
    612  1.1    pooka }
    613  1.1    pooka 
    614  1.1    pooka #else
    615  1.1    pooka #define SysaceDumpRegisters(_c_)
    616  1.1    pooka #endif
    617  1.1    pooka 
    618  1.2  tsutsui /*
    619  1.2  tsutsui  * Reset the device and the interface
    620  1.1    pooka  */
    621  1.2  tsutsui static int
    622  1.2  tsutsui sysace_reset(struct ace_softc *sc)
    623  1.1    pooka {
    624  1.2  tsutsui 	struct _Sac *regs = sc->sc_dr;
    625  1.1    pooka 
    626  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("Sysace::Reset %p\n", sc));
    627  1.1    pooka 
    628  1.2  tsutsui 	/* 16bit etc etc */
    629  1.1    pooka 	uint32_t BusMode, Control;
    630  1.1    pooka 
    631  1.2  tsutsui 	/* reset our interface */
    632  1.2  tsutsui 	regs->Control = SAC_RST;
    633  1.2  tsutsui 	DELAY(200);
    634  1.1    pooka 
    635  1.2  tsutsui 	/* repeat on both byte lanes */
    636  1.1    pooka 	regs->BUSMODEREG = SAC_MODE16 | (SAC_MODE16 << 8);
    637  1.2  tsutsui 	DELAY(1);
    638  1.1    pooka 
    639  1.2  tsutsui 	/* check what our interface does and what the SysACE expects */
    640  1.1    pooka 	Control = regs->Control;
    641  1.1    pooka 	BusMode = regs->BUSMODEREG;
    642  1.1    pooka 
    643  1.2  tsutsui 	/* get them to agree */
    644  1.2  tsutsui 	if (BusMode & SAC_MODE16) {
    645  1.1    pooka 		regs->Control = Control | SAC_BUS16;
    646  1.1    pooka 		regs->Control = regs->Control & ~SAC_BUS8;
    647  1.2  tsutsui 	} else {
    648  1.1    pooka 		regs->Control = Control | SAC_BUS8;
    649  1.1    pooka 		regs->Control = regs->Control & ~SAC_BUS16;
    650  1.1    pooka 	}
    651  1.1    pooka 
    652  1.2  tsutsui 	/* check that it worked */
    653  1.1    pooka 	BusMode = regs->BUSMODEREG;
    654  1.1    pooka 	Control = regs->Control;
    655  1.1    pooka 
    656  1.2  tsutsui 	if (((BusMode & SAC_MODE16) == 0) && ((Control & SAC_BUS8) == 0))
    657  1.2  tsutsui 		return EDOOFUS;
    658  1.2  tsutsui 	if (((BusMode & SAC_MODE16) > 0) && ((Control & SAC_BUS16) == 0))
    659  1.2  tsutsui 		return EDOOFUS;
    660  1.2  tsutsui 
    661  1.2  tsutsui 	/* interrupts off for now */
    662  1.2  tsutsui 	regs->Control &= ~SAC_INTMASK;
    663  1.2  tsutsui #define SAC_INTERRUPTS (SAC_DATABUFRDYIRQ | SAC_ERRORIRQ /* | SAC_CFGDONEIRQ */)
    664  1.2  tsutsui 	Control = regs->CONTROLREG;
    665  1.2  tsutsui 	Control = (Control & ~SAC_INTERRUPTS) | SAC_RESETIRQ | SAC_FORCECFGMODE;
    666  1.2  tsutsui 	regs->CONTROLREG = Control;
    667  1.2  tsutsui 	regs->CONTROLREG = Control & ~SAC_RESETIRQ;
    668  1.1    pooka 
    669  1.2  tsutsui 	/* no command */
    670  1.2  tsutsui 	regs->MPULBAREG = 0;
    671  1.1    pooka 
    672  1.2  tsutsui 	return 0;
    673  1.1    pooka }
    674  1.1    pooka 
    675  1.2  tsutsui /*
    676  1.2  tsutsui  * Take control of the ACE datapath
    677  1.1    pooka  */
    678  1.2  tsutsui static int
    679  1.2  tsutsui sysace_lock_registers(struct ace_softc *sc)
    680  1.1    pooka {
    681  1.2  tsutsui 	uint32_t Status;
    682  1.2  tsutsui 	int i;
    683  1.1    pooka 
    684  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("Sysace::Lock %p\n", sc));
    685  1.1    pooka 
    686  1.2  tsutsui 	/*
    687  1.2  tsutsui 	 * Locked already?
    688  1.2  tsutsui 	 */
    689  1.2  tsutsui 	Status = sc->sc_dr->STATUS;
    690  1.2  tsutsui 	if (Status & SAC_MPULOCK)
    691  1.2  tsutsui 		return TRUE;
    692  1.1    pooka 
    693  1.2  tsutsui 	/*
    694  1.2  tsutsui 	 * Request lock
    695  1.2  tsutsui 	 */
    696  1.2  tsutsui 	sc->sc_dr->CONTROLREG |= SAC_LOCKREQ;
    697  1.1    pooka 
    698  1.2  tsutsui 	/*
    699  1.2  tsutsui 	 * Spin a bit until we get it
    700  1.2  tsutsui 	 */
    701  1.2  tsutsui 	for (i = 0; i < 200; i++) {
    702  1.2  tsutsui 		Status = sc->sc_dr->STATUS;
    703  1.2  tsutsui 		if (Status & SAC_MPULOCK)
    704  1.2  tsutsui 			return TRUE;
    705  1.2  tsutsui 		DELAY(100);
    706  1.2  tsutsui 		DBGME(DEBUG_FUNCS,
    707  1.2  tsutsui 		    printf("Sysace::Lock loops.. (st=%x)\n",Status));
    708  1.2  tsutsui 	}
    709  1.1    pooka 
    710  1.2  tsutsui 	/*
    711  1.2  tsutsui 	 * oopsie!
    712  1.2  tsutsui 	 */
    713  1.2  tsutsui 	DBGME(DEBUG_ERRORS, printf("Sysace::Lock timeout (st=%x)\n",Status));
    714  1.2  tsutsui 	SysaceDumpRegisters(sc->sc_dr);
    715  1.2  tsutsui 	return FALSE;
    716  1.1    pooka }
    717  1.1    pooka 
    718  1.2  tsutsui /*
    719  1.2  tsutsui  * Release control of the ACE datapath
    720  1.1    pooka  */
    721  1.2  tsutsui static int
    722  1.2  tsutsui sysace_unlock_registers(struct ace_softc *sc)
    723  1.1    pooka {
    724  1.2  tsutsui 	uint32_t Status;
    725  1.2  tsutsui 	int i;
    726  1.1    pooka 
    727  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("Sysace::Unlock %p\n", sc));
    728  1.1    pooka 
    729  1.2  tsutsui 	/*
    730  1.2  tsutsui 	 * Clear reset
    731  1.2  tsutsui 	 */
    732  1.2  tsutsui 	sc->sc_dr->CONTROLREG &= ~SAC_CFGRESET;
    733  1.1    pooka 
    734  1.2  tsutsui 	/*
    735  1.2  tsutsui 	 * Unlocked already?
    736  1.2  tsutsui 	 */
    737  1.2  tsutsui 	Status = sc->sc_dr->STATUS;
    738  1.2  tsutsui 	if ((Status & SAC_MPULOCK) == 0)
    739  1.2  tsutsui 		return TRUE;
    740  1.1    pooka 
    741  1.2  tsutsui 	/*
    742  1.2  tsutsui 	 * Request unlock
    743  1.2  tsutsui 	 */
    744  1.2  tsutsui 	sc->sc_dr->CONTROLREG &= ~SAC_LOCKREQ;
    745  1.1    pooka 
    746  1.2  tsutsui 	/*
    747  1.2  tsutsui 	 * Spin a bit until we get it
    748  1.2  tsutsui 	 */
    749  1.2  tsutsui 	for (i = 0; i < 200; i++) {
    750  1.2  tsutsui 		Status = sc->sc_dr->STATUS;
    751  1.2  tsutsui 		if ((Status & SAC_MPULOCK) == 0)
    752  1.2  tsutsui 			return TRUE;
    753  1.2  tsutsui 		DELAY(100);
    754  1.2  tsutsui 		DBGME(DEBUG_FUNCS,
    755  1.2  tsutsui 		    printf("Sysace::Unlock loops.. (st=%x)\n",Status));
    756  1.2  tsutsui 	}
    757  1.1    pooka 
    758  1.2  tsutsui 	/*
    759  1.2  tsutsui 	 * oopsie!
    760  1.2  tsutsui 	 */
    761  1.2  tsutsui 	DBGME(DEBUG_ERRORS, printf("Sysace::Unlock timeout (st=%x)\n",Status));
    762  1.2  tsutsui 	SysaceDumpRegisters(sc->sc_dr);
    763  1.2  tsutsui 	return FALSE;
    764  1.1    pooka }
    765  1.1    pooka 
    766  1.2  tsutsui /*
    767  1.2  tsutsui  * Check if the ACE is waiting for a comamnd
    768  1.1    pooka  */
    769  1.1    pooka #define sysace_ready(_s_) ((_s_)->sc_dr->STATUS & SAC_RDYFORCFCMD)
    770  1.1    pooka 
    771  1.2  tsutsui /*
    772  1.2  tsutsui  * Check if the ACE is executing a comamnd
    773  1.1    pooka  */
    774  1.1    pooka #define sysace_busy(_s_) ((_s_)->sc_dr->STATUS & SAC_CFBSY)
    775  1.1    pooka 
    776  1.2  tsutsui /*
    777  1.2  tsutsui  * Turn on interrupts from the ACE
    778  1.1    pooka  */
    779  1.1    pooka #define sysace_inton(_s_) { \
    780  1.2  tsutsui 	(_s_)->sc_dr->CONTROLREG |= SAC_INTERRUPTS; \
    781  1.2  tsutsui 	(_s_)->sc_dr->Control |= SAC_INTMASK; \
    782  1.1    pooka }
    783  1.1    pooka 
    784  1.2  tsutsui /*
    785  1.2  tsutsui  * Turn off interrupts from the ACE
    786  1.1    pooka  */
    787  1.1    pooka #define sysace_intoff(_s_) { \
    788  1.2  tsutsui 	(_s_)->sc_dr->CONTROLREG &= ~SAC_INTERRUPTS; \
    789  1.2  tsutsui 	(_s_)->sc_dr->Control &= ~SAC_INTMASK; \
    790  1.1    pooka }
    791  1.1    pooka 
    792  1.2  tsutsui /*
    793  1.2  tsutsui  * Start a command on the ACE, such as read or identify.
    794  1.1    pooka  */
    795  1.2  tsutsui static int
    796  1.2  tsutsui sysace_start(struct ace_softc *sc, uint32_t Command, uint32_t Lba,
    797  1.2  tsutsui     uint32_t nSectors)
    798  1.2  tsutsui {
    799  1.2  tsutsui 
    800  1.2  tsutsui 	/*
    801  1.2  tsutsui 	 * Lock it if not already
    802  1.2  tsutsui 	 */
    803  1.2  tsutsui 	if (!sysace_lock_registers(sc)) {
    804  1.2  tsutsui 		/* printed already */
    805  1.2  tsutsui 		return ETIMEDOUT;
    806  1.2  tsutsui 	}
    807  1.2  tsutsui 
    808  1.2  tsutsui 	/*
    809  1.2  tsutsui 	 * Is there a CF inserted
    810  1.2  tsutsui 	 */
    811  1.2  tsutsui 	if (!(sc->sc_dr->STATUS & SAC_CFDETECT)) {
    812  1.2  tsutsui 		/* NB: Not a failure state */
    813  1.2  tsutsui 		DBGME(DEBUG_ERRORS,
    814  1.2  tsutsui 		    printf("Sysace:: no media (st=%x)\n", sc->sc_dr->STATUS));
    815  1.2  tsutsui 		if (sc->sc_capacity) {
    816  1.2  tsutsui 			sc->media_has_changed = TRUE;
    817  1.2  tsutsui 			sc->sc_capacity = 0;
    818  1.2  tsutsui 		}
    819  1.2  tsutsui 		return ENODEV;
    820  1.2  tsutsui 	}
    821  1.2  tsutsui 
    822  1.2  tsutsui 	/*
    823  1.2  tsutsui 	 * Is it ready for a command
    824  1.2  tsutsui 	 */
    825  1.2  tsutsui 	if (!sysace_ready(sc)) {
    826  1.2  tsutsui 		DBGME(DEBUG_ERRORS,
    827  1.2  tsutsui 		    printf("Sysace:: not ready (st=%x)\n", sc->sc_dr->STATUS));
    828  1.2  tsutsui 		SysaceDumpRegisters(sc->sc_dr);
    829  1.2  tsutsui 		return EBUSY;
    830  1.2  tsutsui 	}
    831  1.2  tsutsui 
    832  1.2  tsutsui 	/*
    833  1.2  tsutsui 	 * sector number and command
    834  1.2  tsutsui 	 */
    835  1.2  tsutsui 	sc->sc_dr->MPULBAREG = Lba;
    836  1.2  tsutsui 	sc->sc_dr->SECCNTCMDREG =
    837  1.2  tsutsui 	    (uint16_t)(Command | (nSectors & SAC_SECCCNT));
    838  1.2  tsutsui 
    839  1.2  tsutsui 	/*
    840  1.2  tsutsui 	 *  re-route the chip
    841  1.2  tsutsui 	 * NB: The "RESET" is actually not much of a misnomer.
    842  1.2  tsutsui 	 * The chip was designed for a one-shot execution at reset time,
    843  1.2  tsutsui 	 * namely loading the configuration data into the FPGA. So.
    844  1.2  tsutsui 	 */
    845  1.2  tsutsui 	sc->hw_busy = TRUE;
    846  1.2  tsutsui 	sc->sc_dr->CONTROLREG |= SAC_CFGRESET;
    847  1.2  tsutsui 	return 0;
    848  1.2  tsutsui }
    849  1.2  tsutsui 
    850  1.2  tsutsui /*
    851  1.2  tsutsui  * Identify the (size of the) CompactFlash card inserted in the slot.
    852  1.2  tsutsui  */
    853  1.2  tsutsui static int
    854  1.2  tsutsui sysace_identify(struct ace_softc *sc)
    855  1.2  tsutsui {
    856  1.2  tsutsui 	PCFLASH_IDENTIFY Identify = &sc->sc_params;
    857  1.2  tsutsui 	uint32_t Status = 0;
    858  1.2  tsutsui 	int i, j, error;
    859  1.2  tsutsui 
    860  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("Sysace::Identify %p\n", sc));
    861  1.2  tsutsui 
    862  1.2  tsutsui 	/*
    863  1.2  tsutsui 	 * Turn on interrupts before we start the command
    864  1.2  tsutsui 	 */
    865  1.2  tsutsui 	sysace_inton(sc); /* BUGBUG we should add polling mode (for dump too) */
    866  1.2  tsutsui 
    867  1.2  tsutsui 	/*
    868  1.2  tsutsui 	 * This will invalidate the ACE's current sector data
    869  1.2  tsutsui 	 */
    870  1.2  tsutsui 	sc->sc_capacity = 0;
    871  1.2  tsutsui 
    872  1.2  tsutsui 	/*
    873  1.2  tsutsui 	 * Get it going
    874  1.2  tsutsui 	 */
    875  1.2  tsutsui 	error = sysace_start(sc, SAC_CMD_IDENTIFYMEMCARD, 0, 1);
    876  1.2  tsutsui 
    877  1.2  tsutsui 	/*
    878  1.2  tsutsui 	 * Wait until its done
    879  1.2  tsutsui 	 */
    880  1.2  tsutsui 	if (!FAILED(error)) {
    881  1.2  tsutsui 
    882  1.2  tsutsui 		/* Might be called during autoconf, no interrupts */
    883  1.2  tsutsui 		if (cold) {
    884  1.2  tsutsui 			do {
    885  1.2  tsutsui 				DELAY(10);
    886  1.2  tsutsui 				Status = sc->sc_dr->STATUS;
    887  1.2  tsutsui 			} while ((Status &
    888  1.2  tsutsui 			    (SAC_DATABUFRDY|SAC_CFCERROR|SAC_CFGERROR)) == 0);
    889  1.2  tsutsui 		} else {
    890  1.2  tsutsui 			while (sc->hw_busy) {
    891  1.2  tsutsui 				DBGME(DEBUG_FUNCS,
    892  1.2  tsutsui 				    printf("Sysace:: cwait.. (st=%x)"
    893  1.2  tsutsui 				    " sizeof=%d\n",
    894  1.2  tsutsui 				    sc->sc_dr->STATUS, sizeof(*Identify)));
    895  1.2  tsutsui 				error = tsleep(&sc->media_has_changed, PRIBIO,
    896  1.2  tsutsui 				    "aceidfy", 0);
    897  1.2  tsutsui 			}
    898  1.2  tsutsui 		}
    899  1.2  tsutsui 
    900  1.2  tsutsui 		/*
    901  1.2  tsutsui 		 * Did it work?
    902  1.2  tsutsui 		 */
    903  1.2  tsutsui 		Status = sc->sc_dr->STATUS;
    904  1.2  tsutsui 
    905  1.2  tsutsui 		if (Status & SAC_DATABUFRDY) {
    906  1.2  tsutsui 
    907  1.2  tsutsui 			/*
    908  1.2  tsutsui 			 * Yes, pull out all the data.
    909  1.2  tsutsui 			 * NB: Until we do so the chip will not be ready for
    910  1.2  tsutsui 			 *     another command
    911  1.2  tsutsui 			 */
    912  1.2  tsutsui 			for (i = 0; i < sizeof(*Identify); i += 4) {
    913  1.2  tsutsui 
    914  1.2  tsutsui 				/*
    915  1.2  tsutsui 				 * Verify the (32-bytes) FIFO has reloaded
    916  1.2  tsutsui 				 */
    917  1.2  tsutsui 				for (j = 0; j < 10; j++) {
    918  1.2  tsutsui 					Status = sc->sc_dr->STATUS;
    919  1.2  tsutsui 					if (Status & SAC_DATABUFRDY)
    920  1.2  tsutsui 						break;
    921  1.2  tsutsui 					DELAY(10);
    922  1.2  tsutsui 				}
    923  1.2  tsutsui 				if (Status & SAC_DATABUFRDY) {
    924  1.2  tsutsui 					uint32_t Data32;
    925  1.2  tsutsui 
    926  1.2  tsutsui 					/*
    927  1.2  tsutsui 					 * This pulls two 16-bit words out of
    928  1.2  tsutsui 					 * the FIFO.
    929  1.2  tsutsui 					 * They are ordered in LE.
    930  1.2  tsutsui 					 * NB: Yes this is different from
    931  1.2  tsutsui 					 *     regular data accesses
    932  1.2  tsutsui 					 */
    933  1.2  tsutsui 					Data32 = sc->sc_dr->DATABUFREG[0];
    934  1.1    pooka #if _BYTE_ORDER == _LITTLE_ENDIAN
    935  1.2  tsutsui 					/* all is fine */
    936  1.1    pooka #else
    937  1.2  tsutsui 					Data32 =
    938  1.2  tsutsui 					    (Data32 >> 16) | (Data32 << 16);
    939  1.1    pooka #endif
    940  1.2  tsutsui 					memcpy(((char *)Identify) + i,
    941  1.2  tsutsui 					    &Data32, 4);
    942  1.2  tsutsui 				} else {
    943  1.2  tsutsui 					/*
    944  1.2  tsutsui 					 * Ooops, what's going on here?
    945  1.2  tsutsui 					 */
    946  1.2  tsutsui 					DBGME(DEBUG_ERRORS,
    947  1.2  tsutsui 					    printf("Sysace::!DATABUFRDY %x\n",
    948  1.2  tsutsui 					    Status));
    949  1.2  tsutsui 					error = EIO;
    950  1.2  tsutsui 					break;
    951  1.2  tsutsui 				}
    952  1.2  tsutsui 			}
    953  1.2  tsutsui 
    954  1.2  tsutsui 			/*
    955  1.2  tsutsui 			 * Make sure we did ok and pick up the relevant info
    956  1.2  tsutsui 			 */
    957  1.2  tsutsui 			if (Status & SAC_DATABUFRDY) {
    958  1.2  tsutsui 				DBGME(DEBUG_XFERS,
    959  1.2  tsutsui 				    device_printf(sc->sc_dev,
    960  1.2  tsutsui 				    "model: %.40s/%.20s\n",
    961  1.2  tsutsui 				    Identify->ModelNumber,
    962  1.2  tsutsui 				    Identify->SerialNumber));
    963  1.2  tsutsui 				if (Identify->Signature == CFLASH_SIGNATURE) {
    964  1.2  tsutsui 					DBGME(DEBUG_PROBE,
    965  1.2  tsutsui 					    printf("Sysace::Card is"
    966  1.2  tsutsui 					    " %.40s::%.20s\n",
    967  1.2  tsutsui 					    Identify->ModelNumber,
    968  1.2  tsutsui 					    Identify->SerialNumber));
    969  1.2  tsutsui 
    970  1.2  tsutsui 					sc->sc_capacity =
    971  1.2  tsutsui 					    (Identify->SectorsPerCard[0] << 16)
    972  1.2  tsutsui 					    | Identify->SectorsPerCard[1];
    973  1.2  tsutsui 					DBGME(DEBUG_PROBE,
    974  1.2  tsutsui 					    printf("Sysace::sc_capacity x%qx\n",
    975  1.2  tsutsui 					    sc->sc_capacity));
    976  1.2  tsutsui 					ace_params_to_properties(sc);
    977  1.2  tsutsui 				} else {
    978  1.2  tsutsui 					DBGME(DEBUG_ERRORS,
    979  1.2  tsutsui 					    printf("Sysace::Bad card signature?"
    980  1.2  tsutsui 					    " %x != %x\n",
    981  1.2  tsutsui 					    Identify->Signature,
    982  1.2  tsutsui 					    CFLASH_SIGNATURE));
    983  1.2  tsutsui 					sc->sc_capacity = 0;
    984  1.2  tsutsui 					error = ENXIO;
    985  1.2  tsutsui 				}
    986  1.2  tsutsui 			} else {
    987  1.2  tsutsui 				error = ETIMEDOUT;
    988  1.2  tsutsui 			}
    989  1.2  tsutsui 		} else {
    990  1.2  tsutsui 			/*
    991  1.2  tsutsui 			 * No, it did not work. Maybe there is no card inserted
    992  1.2  tsutsui 			 */
    993  1.2  tsutsui 			DBGME(DEBUG_ERRORS,
    994  1.2  tsutsui 			    printf("Sysace::Identify failed,"
    995  1.2  tsutsui 			    " missing CFLASH card?\n"));
    996  1.2  tsutsui 			SysaceDumpRegisters(sc->sc_dr);
    997  1.2  tsutsui 			/* BUGBUG Fix the error code accordingly */
    998  1.2  tsutsui 			error = ETIMEDOUT;
    999  1.2  tsutsui 		}
   1000  1.2  tsutsui 	}
   1001  1.2  tsutsui 
   1002  1.2  tsutsui 	/* remember this jic */
   1003  1.2  tsutsui 	sc->sc_bio.r_error = Status;
   1004  1.2  tsutsui 
   1005  1.2  tsutsui 	/* Free the ACE for the JTAG, just in case */
   1006  1.2  tsutsui 	sysace_unlock_registers(sc);
   1007  1.2  tsutsui 
   1008  1.2  tsutsui 	/*
   1009  1.2  tsutsui 	 * Done
   1010  1.2  tsutsui 	 */
   1011  1.2  tsutsui 	return error;
   1012  1.2  tsutsui }
   1013  1.2  tsutsui 
   1014  1.2  tsutsui /*
   1015  1.2  tsutsui  * Common code for read&write argument validation
   1016  1.2  tsutsui  */
   1017  1.2  tsutsui static int
   1018  1.2  tsutsui sysace_validate(struct ace_softc *sc, daddr_t start, size_t *pSize)
   1019  1.2  tsutsui {
   1020  1.2  tsutsui 	daddr_t Size;
   1021  1.2  tsutsui 
   1022  1.2  tsutsui 	/*
   1023  1.2  tsutsui 	 * Verify that we know the media size
   1024  1.2  tsutsui 	 */
   1025  1.2  tsutsui 	if (sc->sc_capacity == 0) {
   1026  1.2  tsutsui 		int error = sysace_identify(sc);
   1027  1.2  tsutsui 		if (FAILED(error))
   1028  1.2  tsutsui 			return error;
   1029  1.2  tsutsui 	}
   1030  1.2  tsutsui 
   1031  1.2  tsutsui 	/*
   1032  1.2  tsutsui 	 * Validate args
   1033  1.2  tsutsui 	 */
   1034  1.2  tsutsui 	if (start >= sc->sc_capacity) {
   1035  1.2  tsutsui 		*pSize = 0;
   1036  1.2  tsutsui 		DBGME(DEBUG_ERRORS,
   1037  1.2  tsutsui 		    printf("Sysace::ValidateArg(%qx) EOF\n", start));
   1038  1.2  tsutsui 		return E2BIG;
   1039  1.2  tsutsui 	}
   1040  1.1    pooka 
   1041  1.2  tsutsui 	/*
   1042  1.2  tsutsui 	 * Adjust size if necessary
   1043  1.2  tsutsui 	 */
   1044  1.2  tsutsui 	Size = start + *pSize;
   1045  1.2  tsutsui 	if (Size > sc->sc_capacity) {
   1046  1.2  tsutsui 		/*
   1047  1.2  tsutsui 		 * At most this many sectors
   1048  1.2  tsutsui 		 */
   1049  1.2  tsutsui 		Size = sc->sc_capacity - start;
   1050  1.2  tsutsui 		*pSize = (size_t)Size;
   1051  1.2  tsutsui 	}
   1052  1.2  tsutsui 
   1053  1.2  tsutsui 	DBGME(DEBUG_FUNCS,
   1054  1.2  tsutsui 	    printf("Sysace::Validate %qx %zd\n", start, *pSize));
   1055  1.2  tsutsui 	return 0;
   1056  1.1    pooka }
   1057  1.1    pooka 
   1058  1.1    pooka /* Read SIZE bytes from sysace device, at offset Position
   1059  1.1    pooka  */
   1060  1.1    pooka uint32_t ace_maxatatime = 255;
   1061  1.1    pooka #define MAXATATIME ace_maxatatime //255 /* BUGBUG test me on real hardware!! */
   1062  1.1    pooka 
   1063  1.2  tsutsui static int
   1064  1.2  tsutsui sysace_read_at(struct ace_softc *sc, daddr_t start_sector, char *buffer,
   1065  1.2  tsutsui     size_t nblocks, size_t *pSizeRead)
   1066  1.2  tsutsui {
   1067  1.2  tsutsui 	int error;
   1068  1.2  tsutsui 	uint32_t BlocksThisTime;
   1069  1.2  tsutsui 	uint32_t Status = 0, SizeRead = 0;
   1070  1.2  tsutsui 	uint32_t i, j;
   1071  1.2  tsutsui 
   1072  1.2  tsutsui 	DBGME(DEBUG_XFERS|DEBUG_READS,
   1073  1.2  tsutsui 	    printf("SysaceReadAt(%p %qx %p %zd %p)\n",
   1074  1.2  tsutsui 	    sc, start_sector, buffer, nblocks, pSizeRead));
   1075  1.2  tsutsui 
   1076  1.2  tsutsui 	/*
   1077  1.2  tsutsui 	 * Validate & trim arguments
   1078  1.2  tsutsui 	 */
   1079  1.2  tsutsui 	error = sysace_validate(sc, start_sector, &nblocks);
   1080  1.2  tsutsui 
   1081  1.2  tsutsui 	/*
   1082  1.2  tsutsui 	 * Repeat until we are done or error
   1083  1.2  tsutsui 	 */
   1084  1.2  tsutsui 	while (error == 0) {
   1085  1.2  tsutsui 
   1086  1.2  tsutsui 		/*
   1087  1.2  tsutsui 		 * .. one bunch of sectors at a time
   1088  1.2  tsutsui 		 */
   1089  1.2  tsutsui 		BlocksThisTime = nblocks;
   1090  1.2  tsutsui 		if (BlocksThisTime > MAXATATIME)
   1091  1.2  tsutsui 			BlocksThisTime = MAXATATIME;
   1092  1.2  tsutsui 
   1093  1.2  tsutsui 		/*
   1094  1.2  tsutsui 		 * Yes, start a sector read
   1095  1.2  tsutsui 		 */
   1096  1.2  tsutsui 		sysace_inton(sc);
   1097  1.2  tsutsui 		error = sysace_start(sc,
   1098  1.2  tsutsui 		    SAC_CMD_READMEMCARDDATA,
   1099  1.2  tsutsui 		    (uint32_t)start_sector,  /* BUGBUG trims here, no warn. */
   1100  1.2  tsutsui 		    BlocksThisTime);
   1101  1.2  tsutsui 		/*
   1102  1.2  tsutsui 		 * And wait until done, if ok
   1103  1.2  tsutsui 		 */
   1104  1.2  tsutsui 		if (!FAILED(error)) {
   1105  1.2  tsutsui 			start_sector += BlocksThisTime;
   1106  1.2  tsutsui 			/* Might be called during autoconf, no interrupts */
   1107  1.2  tsutsui 			/* BUGBUG timeouts! */
   1108  1.2  tsutsui 			if (cold) {
   1109  1.2  tsutsui 				do {
   1110  1.2  tsutsui 					DELAY(10);
   1111  1.2  tsutsui 					Status = sc->sc_dr->STATUS;
   1112  1.2  tsutsui 				} while ((Status &
   1113  1.2  tsutsui 				    (SAC_DATABUFRDY|SAC_CFCERROR|SAC_CFGERROR))
   1114  1.2  tsutsui 				    == 0);
   1115  1.2  tsutsui 			} else {
   1116  1.2  tsutsui 				while (sc->hw_busy) {
   1117  1.2  tsutsui 					error = tsleep(&sc->media_has_changed,
   1118  1.2  tsutsui 					    PRIBIO, "aceread", 0);
   1119  1.2  tsutsui 				}
   1120  1.2  tsutsui 			}
   1121  1.2  tsutsui 		}
   1122  1.2  tsutsui 
   1123  1.2  tsutsui 		/*
   1124  1.2  tsutsui 		 * Are we doing ok
   1125  1.2  tsutsui 		 */
   1126  1.2  tsutsui 		if (!FAILED(error)) {
   1127  1.2  tsutsui 
   1128  1.2  tsutsui 		/*
   1129  1.2  tsutsui 		 * Get the data out of the ACE
   1130  1.2  tsutsui 		 */
   1131  1.2  tsutsui 			for (i = 0; i < (BlocksThisTime << CF_SECBITS);
   1132  1.2  tsutsui 			    i += 4) {
   1133  1.2  tsutsui 
   1134  1.2  tsutsui 				/*
   1135  1.2  tsutsui 				 * Make sure the FIFO is ready
   1136  1.2  tsutsui 				 */
   1137  1.2  tsutsui 				for (j = 0; j < 10; j++) {
   1138  1.2  tsutsui 					Status = sc->sc_dr->STATUS;
   1139  1.2  tsutsui 					if (Status & SAC_DATABUFRDY)
   1140  1.2  tsutsui 						break;
   1141  1.2  tsutsui 					DELAY(1000);
   1142  1.2  tsutsui 				}
   1143  1.2  tsutsui 
   1144  1.2  tsutsui 				/*
   1145  1.2  tsutsui 				 * Got it?
   1146  1.2  tsutsui 				 */
   1147  1.2  tsutsui 				if (Status & SAC_DATABUFRDY) {
   1148  1.2  tsutsui 					uint32_t Data32;
   1149  1.2  tsutsui 
   1150  1.2  tsutsui 					Data32 = sc->sc_dr->DATABUFREG[0];
   1151  1.2  tsutsui 					Data32 = le32toh(Data32);
   1152  1.2  tsutsui 					memcpy(buffer + i, &Data32, 4);
   1153  1.2  tsutsui 				} else {
   1154  1.2  tsutsui 					/*
   1155  1.2  tsutsui 					 * Ooops, get out of here
   1156  1.2  tsutsui 					 */
   1157  1.2  tsutsui 					DBGME(DEBUG_ERRORS,
   1158  1.2  tsutsui 					    printf("Sysace::READ timeout\n"));
   1159  1.2  tsutsui 					SysaceDumpRegisters(sc->sc_dr);
   1160  1.2  tsutsui 					error = ETIMEDOUT;
   1161  1.2  tsutsui 					break;
   1162  1.2  tsutsui 				}
   1163  1.2  tsutsui 			}
   1164  1.2  tsutsui 
   1165  1.2  tsutsui 			/*
   1166  1.2  tsutsui 			 * Still doing ok?
   1167  1.2  tsutsui 			 */
   1168  1.2  tsutsui 			if (!FAILED(error)) {
   1169  1.2  tsutsui 				nblocks   -= BlocksThisTime;
   1170  1.2  tsutsui 				SizeRead  += BlocksThisTime;
   1171  1.2  tsutsui 				buffer    += BlocksThisTime << CF_SECBITS;
   1172  1.2  tsutsui 			} else {
   1173  1.2  tsutsui 				/* remember this jic */
   1174  1.2  tsutsui 				sc->sc_bio.r_error = Status;
   1175  1.2  tsutsui 			}
   1176  1.2  tsutsui 		}
   1177  1.2  tsutsui 
   1178  1.2  tsutsui 		/* Free the ACE for the JTAG, just in case */
   1179  1.2  tsutsui 		sysace_unlock_registers(sc);
   1180  1.2  tsutsui 
   1181  1.2  tsutsui 		/*
   1182  1.2  tsutsui 		 * Are we done yet?
   1183  1.2  tsutsui 		 */
   1184  1.2  tsutsui 		if (nblocks == 0)
   1185  1.2  tsutsui 			break;
   1186  1.2  tsutsui 	}
   1187  1.2  tsutsui 
   1188  1.2  tsutsui 	if (pSizeRead)
   1189  1.2  tsutsui 		*pSizeRead = SizeRead;
   1190  1.2  tsutsui 	return error;
   1191  1.2  tsutsui }
   1192  1.2  tsutsui 
   1193  1.2  tsutsui /*
   1194  1.2  tsutsui  * Write SIZE bytes to device.
   1195  1.2  tsutsui  */
   1196  1.2  tsutsui static int
   1197  1.2  tsutsui sysace_write_at(struct ace_softc *sc, daddr_t start_sector, char *buffer,
   1198  1.2  tsutsui     size_t nblocks, size_t *pSizeWritten)
   1199  1.2  tsutsui {
   1200  1.2  tsutsui 	int error;
   1201  1.2  tsutsui 	uint32_t BlocksThisTime;
   1202  1.2  tsutsui 	uint32_t Status = 0, SizeWritten = 0;
   1203  1.2  tsutsui 	uint32_t i, j;
   1204  1.2  tsutsui 
   1205  1.2  tsutsui 	DBGME(DEBUG_XFERS|DEBUG_WRITES,
   1206  1.2  tsutsui 	    printf("SysaceWriteAt(%p %qx %p %zd %p)\n",
   1207  1.2  tsutsui 	    sc, start_sector, buffer, nblocks, pSizeWritten));
   1208  1.2  tsutsui 
   1209  1.2  tsutsui 	/*
   1210  1.2  tsutsui 	 * Validate & trim arguments
   1211  1.2  tsutsui 	 */
   1212  1.2  tsutsui 	error = sysace_validate(sc, start_sector, &nblocks);
   1213  1.2  tsutsui 
   1214  1.2  tsutsui 	/*
   1215  1.2  tsutsui 	 * Repeat until we are done or error
   1216  1.2  tsutsui 	 */
   1217  1.2  tsutsui 	while (error == 0) {
   1218  1.2  tsutsui 
   1219  1.2  tsutsui 		/*
   1220  1.2  tsutsui 		 * .. one sector at a time
   1221  1.2  tsutsui 		 * BUGBUG Supposedly we can do up to 256 sectors?
   1222  1.2  tsutsui 		 */
   1223  1.2  tsutsui 		BlocksThisTime = nblocks;
   1224  1.2  tsutsui 		if (BlocksThisTime > MAXATATIME)
   1225  1.2  tsutsui 			BlocksThisTime = MAXATATIME;
   1226  1.2  tsutsui 
   1227  1.2  tsutsui 		/*
   1228  1.2  tsutsui 		 * Yes, start a sector write
   1229  1.2  tsutsui 		 */
   1230  1.2  tsutsui 		sysace_inton(sc);
   1231  1.2  tsutsui 		error = sysace_start(sc,
   1232  1.2  tsutsui 		    SAC_CMD_WRITEMEMCARDDATA,
   1233  1.2  tsutsui 		    (uint32_t)start_sector,  /* BUGBUG trims here, no warn. */
   1234  1.2  tsutsui 		    BlocksThisTime);
   1235  1.2  tsutsui 		/*
   1236  1.2  tsutsui 		 * And wait until done, if ok
   1237  1.2  tsutsui 		 */
   1238  1.2  tsutsui 		if (!FAILED(error)) {
   1239  1.2  tsutsui 			start_sector += BlocksThisTime;
   1240  1.2  tsutsui 			/* BUGBUG timeouts! */
   1241  1.2  tsutsui 			while (sc->hw_busy) {
   1242  1.2  tsutsui 				error = tsleep(&sc->media_has_changed,
   1243  1.2  tsutsui 				    PRIBIO, "acewrite", 0);
   1244  1.2  tsutsui 			}
   1245  1.2  tsutsui 		}
   1246  1.2  tsutsui 
   1247  1.2  tsutsui 		/*
   1248  1.2  tsutsui 		 * Are we doing ok
   1249  1.2  tsutsui 		 */
   1250  1.2  tsutsui 		if (!FAILED(error)) {
   1251  1.2  tsutsui 
   1252  1.2  tsutsui 			/*
   1253  1.2  tsutsui 			 * Get the data out to the ACE
   1254  1.2  tsutsui 			 */
   1255  1.2  tsutsui 			for (i = 0; i < (BlocksThisTime << CF_SECBITS);
   1256  1.2  tsutsui 			    i += 4) {
   1257  1.2  tsutsui 
   1258  1.2  tsutsui 				/*
   1259  1.2  tsutsui 				 * Make sure the FIFO is ready
   1260  1.2  tsutsui 				 */
   1261  1.2  tsutsui 				for (j = 0; j < 10; j++) {
   1262  1.2  tsutsui 					Status = sc->sc_dr->STATUS;
   1263  1.2  tsutsui 					if (Status & SAC_DATABUFRDY)
   1264  1.2  tsutsui 						break;
   1265  1.2  tsutsui 					DELAY(1000);
   1266  1.2  tsutsui 				}
   1267  1.2  tsutsui 
   1268  1.2  tsutsui 				/*
   1269  1.2  tsutsui 				 * Got it?
   1270  1.2  tsutsui 				 */
   1271  1.2  tsutsui 				if (Status & SAC_DATABUFRDY) {
   1272  1.2  tsutsui 					uint32_t Data32;
   1273  1.2  tsutsui 
   1274  1.2  tsutsui 					memcpy(&Data32, buffer + i, 4);
   1275  1.2  tsutsui 					Data32 = htole32(Data32);
   1276  1.2  tsutsui 					sc->sc_dr->DATABUFREG[0] = Data32;
   1277  1.2  tsutsui 				} else {
   1278  1.2  tsutsui 					/*
   1279  1.2  tsutsui 					 * Ooops, get out of here
   1280  1.2  tsutsui 					 */
   1281  1.2  tsutsui 					DBGME(DEBUG_ERRORS,
   1282  1.2  tsutsui 					    printf("Sysace::WRITE timeout\n"));
   1283  1.2  tsutsui 					SysaceDumpRegisters(sc->sc_dr);
   1284  1.2  tsutsui 					error = ETIMEDOUT;
   1285  1.2  tsutsui 					/* remember this jic */
   1286  1.2  tsutsui 					sc->sc_bio.r_error = Status;
   1287  1.2  tsutsui 					break;
   1288  1.2  tsutsui 				}
   1289  1.2  tsutsui 			}
   1290  1.2  tsutsui 
   1291  1.2  tsutsui 			/*
   1292  1.2  tsutsui 			 * Still doing ok?
   1293  1.2  tsutsui 			 */
   1294  1.2  tsutsui 			if (!FAILED(error)) {
   1295  1.2  tsutsui 				nblocks     -= BlocksThisTime;
   1296  1.2  tsutsui 				SizeWritten += BlocksThisTime;
   1297  1.2  tsutsui 				buffer      += BlocksThisTime << CF_SECBITS;
   1298  1.2  tsutsui 			}
   1299  1.2  tsutsui 		}
   1300  1.2  tsutsui 
   1301  1.2  tsutsui 		/*
   1302  1.2  tsutsui 		 * We need to wait until the device is ready for the
   1303  1.2  tsutsui 		 * next command
   1304  1.2  tsutsui 		 * Experimentation shows that it can take longer than 10msec.
   1305  1.2  tsutsui 		 */
   1306  1.2  tsutsui 		if (!FAILED(error)) {
   1307  1.2  tsutsui 			for (j = 0; j < 300; j++) {
   1308  1.2  tsutsui 				Status = sc->sc_dr->STATUS;
   1309  1.2  tsutsui 				if (Status & SAC_RDYFORCFCMD)
   1310  1.2  tsutsui 					break;
   1311  1.2  tsutsui 				(void)tsleep(&sc->media_has_changed,
   1312  1.2  tsutsui 				    PRIBIO, "acewrite", 2);
   1313  1.2  tsutsui 			}
   1314  1.2  tsutsui 			if (!(Status & SAC_RDYFORCFCMD)) {
   1315  1.2  tsutsui 				DBGME(DEBUG_ERRORS,
   1316  1.2  tsutsui 				    printf("Sysace::WRITE-COMPLETE timeout"
   1317  1.2  tsutsui 				    " St=%x\n", Status));
   1318  1.2  tsutsui 				SysaceDumpRegisters(sc->sc_dr);
   1319  1.2  tsutsui 				/*
   1320  1.2  tsutsui 				 * Ignore, we'll handle it the next time around
   1321  1.2  tsutsui 				 * BUGBUG To be revised along with non-existant
   1322  1.2  tsutsui 				 * error handling
   1323  1.2  tsutsui 				 */
   1324  1.2  tsutsui 			}
   1325  1.2  tsutsui 		}
   1326  1.2  tsutsui 
   1327  1.2  tsutsui 		/* Free the ACE for the JTAG, just in case */
   1328  1.2  tsutsui 		sysace_unlock_registers(sc);
   1329  1.2  tsutsui 
   1330  1.2  tsutsui 		/*
   1331  1.2  tsutsui 		 * Are we done yet?
   1332  1.2  tsutsui 		 */
   1333  1.2  tsutsui 		if (nblocks == 0)
   1334  1.2  tsutsui 			break;
   1335  1.2  tsutsui 	}
   1336  1.2  tsutsui 
   1337  1.2  tsutsui 	if (pSizeWritten)
   1338  1.2  tsutsui 		*pSizeWritten = SizeWritten;
   1339  1.2  tsutsui 	return error;
   1340  1.1    pooka }
   1341  1.1    pooka 
   1342  1.1    pooka int
   1343  1.1    pooka ace_ebus_intr(void *cookie, void *f)
   1344  1.1    pooka {
   1345  1.1    pooka 	struct ace_softc *sc = cookie;
   1346  1.2  tsutsui 	uint32_t Control;
   1347  1.1    pooka 
   1348  1.2  tsutsui 	/*
   1349  1.2  tsutsui 	 * Turn off interrupts and ACK them
   1350  1.2  tsutsui 	 */
   1351  1.2  tsutsui 	sysace_intoff(sc);
   1352  1.1    pooka 
   1353  1.2  tsutsui 	Control = sc->sc_dr->CONTROLREG & (~(SAC_RESETIRQ|SAC_INTERRUPTS));
   1354  1.2  tsutsui 	sc->sc_dr->CONTROLREG = Control | SAC_RESETIRQ;
   1355  1.2  tsutsui 	sc->sc_dr->CONTROLREG = Control;
   1356  1.1    pooka 
   1357  1.2  tsutsui 	/* ... read status and do whatever ... */
   1358  1.1    pooka 
   1359  1.2  tsutsui 	sc->hw_busy = FALSE;
   1360  1.2  tsutsui 	wakeup(&sc->media_has_changed);
   1361  1.2  tsutsui 	return 1;
   1362  1.1    pooka }
   1363  1.1    pooka 
   1364  1.1    pooka #ifdef USE_ACE_FOR_RECONFIG
   1365  1.2  tsutsui static int
   1366  1.2  tsutsui sysace_send_config(struct ace_softc *sc, uint32_t *Data, unsigned int nBytes)
   1367  1.1    pooka {
   1368  1.2  tsutsui 	struct _Sac *Interface = sc->sc_dr;
   1369  1.2  tsutsui 	unsigned int i, j, nWords;
   1370  1.2  tsutsui 	uint32_t CtlWas;
   1371  1.2  tsutsui 	uint32_t Status;
   1372  1.1    pooka 
   1373  1.2  tsutsui 	CtlWas = Interface->CONTROLREG;
   1374  1.1    pooka 
   1375  1.2  tsutsui 	/* Set the bits but in RESET (pag 49-50 of specs)*/
   1376  1.1    pooka #define CFGCMD (SAC_FORCELOCKREQ | SAC_LOCKREQ | SAC_CFGSEL | \
   1377  1.2  tsutsui 		SAC_FORCECFGMODE |/* SAC_CFGMODE |*/ SAC_CFGSTART)
   1378  1.1    pooka 
   1379  1.2  tsutsui 	Interface->CONTROLREG = CFGCMD | SAC_CFGRESET;
   1380  1.1    pooka 
   1381  1.2  tsutsui 	/* Take it out of RESET */
   1382  1.2  tsutsui 	Interface->CONTROLREG = CFGCMD;
   1383  1.2  tsutsui 
   1384  1.2  tsutsui 	/*
   1385  1.2  tsutsui 	 * Must wait till it says READY
   1386  1.2  tsutsui 	 * It can take a looong time
   1387  1.2  tsutsui 	 */
   1388  1.2  tsutsui 	for (j = 0; j < 1000; j++) {
   1389  1.2  tsutsui 		Status = Interface->STATUS;
   1390  1.2  tsutsui 		if (Status & SAC_RDYFORCFCMD)
   1391  1.2  tsutsui 			break;
   1392  1.2  tsutsui 		DELAY(1000);
   1393  1.2  tsutsui 	}
   1394  1.1    pooka 
   1395  1.2  tsutsui 	if (0 == (Status & SAC_RDYFORCFCMD)) {
   1396  1.2  tsutsui 		DBGME(DEBUG_ERRORS,
   1397  1.2  tsutsui 		    printf("Sysace::CMD error %x (j=%d)\n", Status, j));
   1398  1.2  tsutsui 		goto Error;
   1399  1.2  tsutsui 	}
   1400  1.1    pooka 
   1401  1.2  tsutsui 	/*
   1402  1.2  tsutsui 	 * Get the data out to the ACE
   1403  1.2  tsutsui 	 */
   1404  1.1    pooka #define ACEROUNDUP 32
   1405  1.2  tsutsui 	nBytes = (nBytes + ACEROUNDUP - 1) & ~(ACEROUNDUP-1);
   1406  1.2  tsutsui 	nWords = (nBytes + 3) / 4;
   1407  1.2  tsutsui 	DBGME(DEBUG_FUNCS,
   1408  1.2  tsutsui 	    printf("Sending %d bytes (as %d words) to %p ",
   1409  1.2  tsutsui 	    nBytes, nWords, Interface));
   1410  1.2  tsutsui 	for (i = 0; i < nWords; i += 1/*word*/) {
   1411  1.2  tsutsui 
   1412  1.2  tsutsui 		/* Stop on errors */
   1413  1.2  tsutsui 		Status = Interface->ERRORREG;
   1414  1.2  tsutsui 		if (Status) {
   1415  1.2  tsutsui 			/*
   1416  1.2  tsutsui 			 * Ooops, get out of here
   1417  1.2  tsutsui 			 */
   1418  1.2  tsutsui 			DBGME(DEBUG_ERRORS,
   1419  1.2  tsutsui 			    printf("Sysace::CFG error %x (i=%d)\n", Status, i));
   1420  1.2  tsutsui 			goto Error;
   1421  1.2  tsutsui 		}
   1422  1.2  tsutsui 
   1423  1.2  tsutsui 		/*
   1424  1.2  tsutsui 		 * Make sure the FIFO is ready
   1425  1.2  tsutsui 		 */
   1426  1.2  tsutsui 		for (j = 0; j < 100; j++) {
   1427  1.2  tsutsui 			Status = Interface->STATUS;
   1428  1.2  tsutsui 			if (Status & SAC_DATABUFRDY)
   1429  1.2  tsutsui 				break;
   1430  1.2  tsutsui 			DELAY(1000);
   1431  1.2  tsutsui 		}
   1432  1.2  tsutsui 
   1433  1.2  tsutsui 		/*
   1434  1.2  tsutsui 		 * Got it?
   1435  1.2  tsutsui 		 */
   1436  1.2  tsutsui 		if (Status & SAC_DATABUFRDY) {
   1437  1.2  tsutsui 			uint32_t Data32;
   1438  1.2  tsutsui 
   1439  1.2  tsutsui 			Data32 = Data[i];
   1440  1.2  tsutsui 			Data32 = htole32(Data32);
   1441  1.2  tsutsui 			Interface->DATABUFREG[0] = Data32;
   1442  1.2  tsutsui 		} else {
   1443  1.2  tsutsui 			/*
   1444  1.2  tsutsui 			 * Ooops, get out of here
   1445  1.2  tsutsui 			 */
   1446  1.2  tsutsui 			DBGME(DEBUG_ERRORS,
   1447  1.2  tsutsui 			    printf("Sysace::WRITE timeout %x (i=%d)\n",
   1448  1.2  tsutsui 			    Status, i));
   1449  1.2  tsutsui 			goto Error;
   1450  1.2  tsutsui 		}
   1451  1.2  tsutsui 	}
   1452  1.2  tsutsui 	DBGME(DEBUG_FUNCS, printf("done ok.\n"));
   1453  1.2  tsutsui 
   1454  1.2  tsutsui 	/* Put it back the way it was (try to.. :-( )*/
   1455  1.2  tsutsui 	Interface->CONTROLREG = CtlWas;
   1456  1.2  tsutsui 	return 0;
   1457  1.1    pooka 
   1458  1.1    pooka  Error:
   1459  1.2  tsutsui 	SysaceDumpRegisters(Interface);
   1460  1.2  tsutsui 	Interface->CONTROLREG = CtlWas;
   1461  1.2  tsutsui 	return EIO;
   1462  1.1    pooka }
   1463  1.1    pooka #endif /* USE_ACE_FOR_RECONFIG */
   1464  1.1    pooka 
   1465  1.1    pooka 
   1466  1.2  tsutsui /*
   1467  1.2  tsutsui  * Rest of code lifted with mods from the dev\ata\wd.c driver
   1468  1.1    pooka  */
   1469  1.1    pooka 
   1470  1.2  tsutsui /*	$NetBSD: ace_ebus.c,v 1.2 2011/06/18 17:47:20 tsutsui Exp $ */
   1471  1.1    pooka 
   1472  1.1    pooka /*
   1473  1.1    pooka  * Copyright (c) 1998, 2001 Manuel Bouyer.  All rights reserved.
   1474  1.1    pooka  *
   1475  1.1    pooka  * Redistribution and use in source and binary forms, with or without
   1476  1.1    pooka  * modification, are permitted provided that the following conditions
   1477  1.1    pooka  * are met:
   1478  1.1    pooka  * 1. Redistributions of source code must retain the above copyright
   1479  1.1    pooka  *	notice, this list of conditions and the following disclaimer.
   1480  1.1    pooka  * 2. Redistributions in binary form must reproduce the above copyright
   1481  1.1    pooka  *	notice, this list of conditions and the following disclaimer in the
   1482  1.1    pooka  *	documentation and/or other materials provided with the distribution.
   1483  1.1    pooka  * 3. All advertising materials mentioning features or use of this software
   1484  1.1    pooka  *	must display the following acknowledgement:
   1485  1.1    pooka  *  This product includes software developed by Manuel Bouyer.
   1486  1.1    pooka  * 4. The name of the author may not be used to endorse or promote products
   1487  1.1    pooka  *	derived from this software without specific prior written permission.
   1488  1.1    pooka  *
   1489  1.1    pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   1490  1.1    pooka  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   1491  1.1    pooka  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   1492  1.1    pooka  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   1493  1.1    pooka  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   1494  1.1    pooka  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   1495  1.1    pooka  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   1496  1.1    pooka  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   1497  1.1    pooka  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   1498  1.1    pooka  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   1499  1.1    pooka  */
   1500  1.1    pooka 
   1501  1.1    pooka /*-
   1502  1.1    pooka  * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
   1503  1.1    pooka  * All rights reserved.
   1504  1.1    pooka  *
   1505  1.1    pooka  * This code is derived from software contributed to The NetBSD Foundation
   1506  1.1    pooka  * by Charles M. Hannum and by Onno van der Linden.
   1507  1.1    pooka  *
   1508  1.1    pooka  * Redistribution and use in source and binary forms, with or without
   1509  1.1    pooka  * modification, are permitted provided that the following conditions
   1510  1.1    pooka  * are met:
   1511  1.1    pooka  * 1. Redistributions of source code must retain the above copyright
   1512  1.1    pooka  *    notice, this list of conditions and the following disclaimer.
   1513  1.1    pooka  * 2. Redistributions in binary form must reproduce the above copyright
   1514  1.1    pooka  *    notice, this list of conditions and the following disclaimer in the
   1515  1.1    pooka  *    documentation and/or other materials provided with the distribution.
   1516  1.1    pooka  * 3. All advertising materials mentioning features or use of this software
   1517  1.1    pooka  *    must display the following acknowledgement:
   1518  1.1    pooka  *        This product includes software developed by the NetBSD
   1519  1.1    pooka  *        Foundation, Inc. and its contributors.
   1520  1.1    pooka  * 4. Neither the name of The NetBSD Foundation nor the names of its
   1521  1.1    pooka  *    contributors may be used to endorse or promote products derived
   1522  1.1    pooka  *    from this software without specific prior written permission.
   1523  1.1    pooka  *
   1524  1.1    pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   1525  1.1    pooka  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   1526  1.1    pooka  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   1527  1.1    pooka  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   1528  1.1    pooka  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   1529  1.1    pooka  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   1530  1.1    pooka  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   1531  1.1    pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   1532  1.1    pooka  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   1533  1.1    pooka  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   1534  1.1    pooka  * POSSIBILITY OF SUCH DAMAGE.
   1535  1.1    pooka  */
   1536  1.1    pooka 
   1537  1.1    pooka static const char ST506[] = "ST506";
   1538  1.1    pooka 
   1539  1.1    pooka #define	ACEIORETRIES_SINGLE 4	/* number of retries before single-sector */
   1540  1.1    pooka #define	ACEIORETRIES	5	/* number of retries before giving up */
   1541  1.1    pooka #define	RECOVERYTIME hz/2	/* time to wait before retrying a cmd */
   1542  1.1    pooka 
   1543  1.1    pooka #define	ACEUNIT(dev)		DISKUNIT(dev)
   1544  1.1    pooka #define	ACEPART(dev)		DISKPART(dev)
   1545  1.1    pooka #define	ACEMINOR(unit, part)	DISKMINOR(unit, part)
   1546  1.1    pooka #define	MAKEACEDEV(maj, unit, part)	MAKEDISKDEV(maj, unit, part)
   1547  1.1    pooka 
   1548  1.1    pooka #define	ACELABELDEV(dev)	(MAKEACEDEV(major(dev), ACEUNIT(dev), RAW_PART))
   1549  1.1    pooka 
   1550  1.1    pooka void	aceperror(const struct ace_softc *);
   1551  1.1    pooka 
   1552  1.1    pooka extern struct cfdriver ace_cd;
   1553  1.1    pooka 
   1554  1.1    pooka dev_type_open(aceopen);
   1555  1.1    pooka dev_type_close(aceclose);
   1556  1.1    pooka dev_type_read(aceread);
   1557  1.1    pooka dev_type_write(acewrite);
   1558  1.1    pooka dev_type_ioctl(aceioctl);
   1559  1.1    pooka dev_type_strategy(acestrategy);
   1560  1.1    pooka dev_type_dump(acedump);
   1561  1.1    pooka dev_type_size(acesize);
   1562  1.1    pooka 
   1563  1.1    pooka const struct bdevsw ace_bdevsw = {
   1564  1.1    pooka 	aceopen, aceclose, acestrategy, aceioctl, acedump, acesize, D_DISK
   1565  1.1    pooka };
   1566  1.1    pooka 
   1567  1.1    pooka const struct cdevsw ace_cdevsw = {
   1568  1.1    pooka 	aceopen, aceclose, aceread, acewrite, aceioctl,
   1569  1.1    pooka 	nostop, notty, nopoll, nommap, nokqfilter, D_DISK
   1570  1.1    pooka };
   1571  1.1    pooka 
   1572  1.1    pooka void  acegetdefaultlabel(struct ace_softc *, struct disklabel *);
   1573  1.1    pooka void  acegetdisklabel(struct ace_softc *);
   1574  1.1    pooka void  acestart(void *);
   1575  1.1    pooka void  __acestart(struct ace_softc*, struct buf *);
   1576  1.1    pooka void  acerestart(void *);
   1577  1.1    pooka 
   1578  1.1    pooka struct dkdriver acedkdriver = { acestrategy, minphys };
   1579  1.1    pooka 
   1580  1.1    pooka #ifdef HAS_BAD144_HANDLING
   1581  1.1    pooka static void bad144intern(struct ace_softc *);
   1582  1.1    pooka #endif
   1583  1.1    pooka 
   1584  1.1    pooka void
   1585  1.1    pooka aceattach(struct ace_softc *ace)
   1586  1.1    pooka {
   1587  1.1    pooka 	struct device *self = ace->sc_dev;
   1588  1.1    pooka 	char tbuf[41], pbuf[9], c, *p, *q;
   1589  1.1    pooka 	int i, blank;
   1590  1.1    pooka 	DEBUG_PRINT(("aceattach\n"), DEBUG_FUNCS | DEBUG_PROBE);
   1591  1.1    pooka 
   1592  1.1    pooka 	callout_init(&ace->sc_restart_ch, 0);
   1593  1.1    pooka 	bufq_alloc(&ace->sc_q, BUFQ_DISK_DEFAULT_STRAT, BUFQ_SORT_RAWBLOCK);
   1594  1.1    pooka 
   1595  1.2  tsutsui 	ace->openings = 1; /* wazziz?*/
   1596  1.2  tsutsui 	ace->sc_multi = MAXATATIME;
   1597  1.1    pooka 
   1598  1.1    pooka 	aprint_naive("\n");
   1599  1.1    pooka 
   1600  1.1    pooka 	/* setup all required fields so that if the attach fails we are ok */
   1601  1.1    pooka 	ace->sc_dk.dk_driver = &acedkdriver;
   1602  1.1    pooka 	ace->sc_dk.dk_name = device_xname(ace->sc_dev);
   1603  1.1    pooka 
   1604  1.1    pooka 	/* read our drive info */
   1605  1.1    pooka 	if (sysace_attach(ace) != 0) {
   1606  1.1    pooka 		aprint_error_dev(ace->sc_dev, "attach failed\n");
   1607  1.1    pooka 		return;
   1608  1.1    pooka 	}
   1609  1.1    pooka 
   1610  1.1    pooka 	aprint_normal_dev(ace->sc_dev, "drive supports %d-sector PIO xfers\n",
   1611  1.1    pooka 	    ace->sc_multi);
   1612  1.1    pooka 
   1613  1.1    pooka 	for (blank = 0, p = ace->sc_params.ModelNumber, q = tbuf, i = 0;
   1614  1.1    pooka 	    i < sizeof(ace->sc_params.ModelNumber); i++) {
   1615  1.1    pooka 		c = *p++;
   1616  1.1    pooka 		if (c == '\0')
   1617  1.1    pooka 			break;
   1618  1.1    pooka 		if (c != ' ') {
   1619  1.1    pooka 			if (blank) {
   1620  1.1    pooka 				*q++ = ' ';
   1621  1.1    pooka 				blank = 0;
   1622  1.1    pooka 			}
   1623  1.1    pooka 			*q++ = c;
   1624  1.1    pooka 		} else
   1625  1.1    pooka 			blank = 1;
   1626  1.1    pooka 	}
   1627  1.1    pooka 	*q++ = '\0';
   1628  1.1    pooka 
   1629  1.1    pooka 	aprint_normal_dev(ace->sc_dev, "card is <%s>\n", tbuf);
   1630  1.1    pooka 
   1631  1.1    pooka 	format_bytes(pbuf, sizeof(pbuf), ace->sc_capacity * DEV_BSIZE);
   1632  1.1    pooka 	aprint_normal("%s: %s, %d cyl, %d head, %d sec, "
   1633  1.1    pooka 	    "%d bytes/sect x %llu sectors\n",
   1634  1.1    pooka 	    self->dv_xname, pbuf,
   1635  1.1    pooka 	    (int)(ace->sc_capacity /
   1636  1.2  tsutsui 	    (ace->sc_params.CurrentNumberOfHeads *
   1637  1.2  tsutsui 	    ace->sc_params.CurrentSectorsPerTrack)),
   1638  1.2  tsutsui 	    ace->sc_params.CurrentNumberOfHeads,
   1639  1.2  tsutsui 	    ace->sc_params.CurrentSectorsPerTrack,
   1640  1.1    pooka 	    DEV_BSIZE, (unsigned long long)ace->sc_capacity);
   1641  1.1    pooka 
   1642  1.1    pooka 	/*
   1643  1.1    pooka 	 * Attach the disk structure. We fill in dk_info later.
   1644  1.1    pooka 	 */
   1645  1.1    pooka 	disk_attach(&ace->sc_dk);
   1646  1.1    pooka 
   1647  1.1    pooka #if NRND > 0
   1648  1.1    pooka 	rnd_attach_source(&ace->rnd_source, device_xname(ace->sc_dev),
   1649  1.1    pooka 			  RND_TYPE_DISK, 0);
   1650  1.1    pooka #endif
   1651  1.1    pooka 
   1652  1.1    pooka }
   1653  1.1    pooka 
   1654  1.1    pooka int
   1655  1.1    pooka aceactivate(struct device *self, enum devact act)
   1656  1.1    pooka {
   1657  1.1    pooka 	int rv = 0;
   1658  1.1    pooka 
   1659  1.1    pooka 	switch (act) {
   1660  1.1    pooka 	case DVACT_DEACTIVATE:
   1661  1.1    pooka 		/*
   1662  1.1    pooka 		 * Nothing to do; we key off the device's DVF_ACTIVATE.
   1663  1.1    pooka 		 */
   1664  1.1    pooka 		break;
   1665  1.1    pooka 	default:
   1666  1.1    pooka 		rv = EOPNOTSUPP;
   1667  1.1    pooka 	}
   1668  1.2  tsutsui 	return rv;
   1669  1.1    pooka }
   1670  1.1    pooka 
   1671  1.1    pooka int
   1672  1.1    pooka acedetach(struct device *self, int flags)
   1673  1.1    pooka {
   1674  1.1    pooka 	struct ace_softc *sc = device_private(self);
   1675  1.1    pooka 	int s, bmaj, cmaj, i, mn;
   1676  1.1    pooka 
   1677  1.1    pooka 	/* locate the major number */
   1678  1.1    pooka 	bmaj = bdevsw_lookup_major(&ace_bdevsw);
   1679  1.1    pooka 	cmaj = cdevsw_lookup_major(&ace_cdevsw);
   1680  1.1    pooka 
   1681  1.1    pooka 	/* Nuke the vnodes for any open instances. */
   1682  1.1    pooka 	for (i = 0; i < MAXPARTITIONS; i++) {
   1683  1.1    pooka 		mn = ACEMINOR(device_unit(self), i);
   1684  1.1    pooka 		vdevgone(bmaj, mn, mn, VBLK);
   1685  1.1    pooka 		vdevgone(cmaj, mn, mn, VCHR);
   1686  1.1    pooka 	}
   1687  1.1    pooka 
   1688  1.1    pooka 	/* Delete all of our wedges. */
   1689  1.1    pooka 	dkwedge_delall(&sc->sc_dk);
   1690  1.1    pooka 
   1691  1.1    pooka 	s = splbio();
   1692  1.1    pooka 
   1693  1.1    pooka 	/* Kill off any queued buffers. */
   1694  1.1    pooka 	bufq_drain(sc->sc_q);
   1695  1.1    pooka 
   1696  1.1    pooka 	bufq_free(sc->sc_q);
   1697  1.2  tsutsui #if 0
   1698  1.2  tsutsui 	sc->atabus->ata_killpending(sc->drvp);
   1699  1.2  tsutsui #endif
   1700  1.1    pooka 
   1701  1.1    pooka 	splx(s);
   1702  1.1    pooka 
   1703  1.1    pooka 	/* Detach disk. */
   1704  1.1    pooka 	disk_detach(&sc->sc_dk);
   1705  1.1    pooka 
   1706  1.1    pooka #if NRND > 0
   1707  1.1    pooka 	/* Unhook the entropy source. */
   1708  1.1    pooka 	rnd_detach_source(&sc->rnd_source);
   1709  1.1    pooka #endif
   1710  1.1    pooka 
   1711  1.2  tsutsui #if 0
   1712  1.2  tsutsui 	sc->drvp->drive_flags = 0; /* no drive any more here */
   1713  1.2  tsutsui #endif
   1714  1.1    pooka 
   1715  1.2  tsutsui 	return 0;
   1716  1.1    pooka }
   1717  1.1    pooka 
   1718  1.1    pooka /*
   1719  1.1    pooka  * Read/write routine for a buffer.  Validates the arguments and schedules the
   1720  1.1    pooka  * transfer.  Does not wait for the transfer to complete.
   1721  1.1    pooka  */
   1722  1.1    pooka void
   1723  1.1    pooka acestrategy(struct buf *bp)
   1724  1.1    pooka {
   1725  1.2  tsutsui 	struct ace_softc *ace;
   1726  1.1    pooka 	struct disklabel *lp;
   1727  1.1    pooka 	daddr_t blkno;
   1728  1.1    pooka 	int s;
   1729  1.1    pooka 
   1730  1.2  tsutsui 	ace = device_lookup_private(&ace_cd, ACEUNIT(bp->b_dev));
   1731  1.2  tsutsui 
   1732  1.1    pooka 	if (ace == NULL) {
   1733  1.2  tsutsui 		bp->b_error = ENXIO;
   1734  1.2  tsutsui 		biodone(bp);
   1735  1.2  tsutsui 		return;
   1736  1.1    pooka 	}
   1737  1.1    pooka 	lp = ace->sc_dk.dk_label;
   1738  1.1    pooka 
   1739  1.2  tsutsui 	DEBUG_PRINT(("acestrategy (%s) %lld\n",
   1740  1.2  tsutsui 	    device_xname(ace->sc_dev), bp->b_blkno), DEBUG_XFERS);
   1741  1.1    pooka 
   1742  1.1    pooka 	/* Valid request?  */
   1743  1.1    pooka 	if (bp->b_blkno < 0 ||
   1744  1.1    pooka 	    (bp->b_bcount % lp->d_secsize) != 0 ||
   1745  1.1    pooka 	    (bp->b_bcount / lp->d_secsize) >= (1 << NBBY)) {
   1746  1.1    pooka 		bp->b_error = EINVAL;
   1747  1.1    pooka 		goto done;
   1748  1.1    pooka 	}
   1749  1.1    pooka 
   1750  1.1    pooka 	/* If device invalidated (e.g. media change, door open), error. */
   1751  1.1    pooka 	if ((ace->sc_flags & ACEF_LOADED) == 0) {
   1752  1.1    pooka 		bp->b_error = EIO;
   1753  1.1    pooka 		goto done;
   1754  1.1    pooka 	}
   1755  1.1    pooka 
   1756  1.1    pooka 	/* If it's a null transfer, return immediately. */
   1757  1.1    pooka 	if (bp->b_bcount == 0)
   1758  1.1    pooka 		goto done;
   1759  1.1    pooka 
   1760  1.1    pooka 	/*
   1761  1.1    pooka 	 * Do bounds checking, adjust transfer. if error, process.
   1762  1.1    pooka 	 * If end of partition, just return.
   1763  1.1    pooka 	 */
   1764  1.1    pooka 	if (ACEPART(bp->b_dev) == RAW_PART) {
   1765  1.1    pooka 		if (bounds_check_with_mediasize(bp, DEV_BSIZE,
   1766  1.1    pooka 		    ace->sc_capacity) <= 0)
   1767  1.1    pooka 			goto done;
   1768  1.1    pooka 	} else {
   1769  1.1    pooka 		if (bounds_check_with_label(&ace->sc_dk, bp,
   1770  1.1    pooka 		    (ace->sc_flags & (ACEF_WLABEL|ACEF_LABELLING)) != 0) <= 0)
   1771  1.1    pooka 			goto done;
   1772  1.1    pooka 	}
   1773  1.1    pooka 
   1774  1.1    pooka 	/*
   1775  1.1    pooka 	 * Now convert the block number to absolute and put it in
   1776  1.1    pooka 	 * terms of the device's logical block size.
   1777  1.1    pooka 	 */
   1778  1.1    pooka 	if (lp->d_secsize >= DEV_BSIZE)
   1779  1.1    pooka 		blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
   1780  1.1    pooka 	else
   1781  1.1    pooka 		blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
   1782  1.1    pooka 
   1783  1.1    pooka 	if (ACEPART(bp->b_dev) != RAW_PART)
   1784  1.1    pooka 		blkno += lp->d_partitions[ACEPART(bp->b_dev)].p_offset;
   1785  1.1    pooka 
   1786  1.1    pooka 	bp->b_rawblkno = blkno;
   1787  1.1    pooka 
   1788  1.1    pooka 	/* Queue transfer on drive, activate drive and controller if idle. */
   1789  1.1    pooka 	s = splbio();
   1790  1.1    pooka 	bufq_put(ace->sc_q, bp);
   1791  1.1    pooka 	acestart(ace);
   1792  1.1    pooka 	splx(s);
   1793  1.1    pooka 	return;
   1794  1.1    pooka done:
   1795  1.1    pooka 	/* Toss transfer; we're done early. */
   1796  1.1    pooka 	bp->b_resid = bp->b_bcount;
   1797  1.1    pooka 	biodone(bp);
   1798  1.1    pooka }
   1799  1.1    pooka 
   1800  1.1    pooka /*
   1801  1.1    pooka  * Queue a drive for I/O.
   1802  1.1    pooka  */
   1803  1.1    pooka void
   1804  1.1    pooka acestart(void *arg)
   1805  1.1    pooka {
   1806  1.1    pooka 	struct ace_softc *ace = arg;
   1807  1.1    pooka 	struct buf *bp = NULL;
   1808  1.1    pooka 
   1809  1.1    pooka 	DEBUG_PRINT(("acestart %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
   1810  1.1    pooka 	while (ace->openings > 0) {
   1811  1.1    pooka 
   1812  1.1    pooka 		/* Is there a buf for us ? */
   1813  1.1    pooka 		if ((bp = bufq_get(ace->sc_q)) == NULL)
   1814  1.1    pooka 			return;
   1815  1.1    pooka 
   1816  1.1    pooka 		/*
   1817  1.1    pooka 		 * Make the command. First lock the device
   1818  1.1    pooka 		 */
   1819  1.1    pooka 		ace->openings--;
   1820  1.1    pooka 
   1821  1.1    pooka 		ace->retries = 0;
   1822  1.1    pooka 		__acestart(ace, bp);
   1823  1.1    pooka 	}
   1824  1.1    pooka }
   1825  1.1    pooka 
   1826  1.1    pooka void
   1827  1.1    pooka __acestart(struct ace_softc *sc, struct buf *bp)
   1828  1.1    pooka {
   1829  1.2  tsutsui 
   1830  1.1    pooka 	sc->sc_bp = bp;
   1831  1.1    pooka 	/*
   1832  1.1    pooka 	 * If we're retrying, retry in single-sector mode. This will give us
   1833  1.1    pooka 	 * the sector number of the problem, and will eventually allow the
   1834  1.1    pooka 	 * transfer to succeed.
   1835  1.1    pooka 	 */
   1836  1.1    pooka 	if (sc->retries >= ACEIORETRIES_SINGLE)
   1837  1.1    pooka 		sc->sc_bio.flags = ATA_SINGLE;
   1838  1.1    pooka 	else
   1839  1.1    pooka 		sc->sc_bio.flags = 0;
   1840  1.1    pooka 	if (bp->b_flags & B_READ)
   1841  1.1    pooka 		sc->sc_bio.flags |= ATA_READ;
   1842  1.1    pooka 	sc->sc_bio.blkno = bp->b_rawblkno;
   1843  1.1    pooka 	sc->sc_bio.blkdone = 0;
   1844  1.1    pooka 	sc->sc_bio.nbytes = bp->b_bcount;
   1845  1.1    pooka 	sc->sc_bio.nblks  = bp->b_bcount >> CF_SECBITS;
   1846  1.1    pooka 	sc->sc_bio.databuf = bp->b_data;
   1847  1.1    pooka 	/* Instrumentation. */
   1848  1.1    pooka 	disk_busy(&sc->sc_dk);
   1849  1.2  tsutsui 	sc->active_xfer = bp;
   1850  1.2  tsutsui 	wakeup(&sc->ch_thread);
   1851  1.1    pooka }
   1852  1.1    pooka 
   1853  1.1    pooka void
   1854  1.1    pooka acedone(struct ace_softc *ace)
   1855  1.1    pooka {
   1856  1.1    pooka 	struct buf *bp = ace->sc_bp;
   1857  1.1    pooka 	const char *errmsg;
   1858  1.1    pooka 	int do_perror = 0;
   1859  1.1    pooka 
   1860  1.1    pooka 	DEBUG_PRINT(("acedone %s\n", device_xname(ace->sc_dev)), DEBUG_XFERS);
   1861  1.1    pooka 
   1862  1.1    pooka 	if (bp == NULL)
   1863  1.1    pooka 		return;
   1864  1.1    pooka 
   1865  1.1    pooka 	bp->b_resid = ace->sc_bio.nbytes;
   1866  1.1    pooka 	switch (ace->sc_bio.error) {
   1867  1.1    pooka 	case ETIMEDOUT:
   1868  1.1    pooka 		errmsg = "device timeout";
   1869  1.2  tsutsui 	do_perror = 1;
   1870  1.1    pooka 		goto retry;
   1871  1.1    pooka 	case EBUSY:
   1872  1.1    pooka 	case EDOOFUS:
   1873  1.1    pooka 		errmsg = "device stuck";
   1874  1.1    pooka retry:		/* Just reset and retry. Can we do more ? */
   1875  1.1    pooka 		sysace_reset(ace);
   1876  1.1    pooka 		diskerr(bp, "ace", errmsg, LOG_PRINTF,
   1877  1.1    pooka 		    ace->sc_bio.blkdone, ace->sc_dk.dk_label);
   1878  1.1    pooka 		if (ace->retries < ACEIORETRIES)
   1879  1.1    pooka 			printf(", retrying");
   1880  1.1    pooka 		printf("\n");
   1881  1.1    pooka 		if (do_perror)
   1882  1.1    pooka 			aceperror(ace);
   1883  1.1    pooka 		if (ace->retries < ACEIORETRIES) {
   1884  1.1    pooka 			ace->retries++;
   1885  1.1    pooka 			callout_reset(&ace->sc_restart_ch, RECOVERYTIME,
   1886  1.1    pooka 			    acerestart, ace);
   1887  1.1    pooka 			return;
   1888  1.1    pooka 		}
   1889  1.1    pooka 
   1890  1.1    pooka 		bp->b_error = EIO;
   1891  1.1    pooka 		break;
   1892  1.1    pooka 	case 0:
   1893  1.2  tsutsui 		if ((ace->sc_bio.flags & ATA_CORR) || ace->retries > 0)
   1894  1.1    pooka 			printf("%s: soft error (corrected)\n",
   1895  1.1    pooka 			    device_xname(ace->sc_dev));
   1896  1.1    pooka 		break;
   1897  1.1    pooka 	case ENODEV:
   1898  1.1    pooka 	case E2BIG:
   1899  1.1    pooka 		bp->b_error = EIO;
   1900  1.1    pooka 		break;
   1901  1.1    pooka 	}
   1902  1.1    pooka 	disk_unbusy(&ace->sc_dk, (bp->b_bcount - bp->b_resid),
   1903  1.1    pooka 	    (bp->b_flags & B_READ));
   1904  1.1    pooka #if NRND > 0
   1905  1.1    pooka 	rnd_add_uint32(&ace->rnd_source, bp->b_blkno);
   1906  1.1    pooka #endif
   1907  1.2  tsutsui 	biodone(bp);
   1908  1.2  tsutsui 	ace->openings++;
   1909  1.1    pooka 	acestart(ace);
   1910  1.1    pooka }
   1911  1.1    pooka 
   1912  1.1    pooka void
   1913  1.1    pooka acerestart(void *v)
   1914  1.1    pooka {
   1915  1.1    pooka 	struct ace_softc *ace = v;
   1916  1.1    pooka 	struct buf *bp = ace->sc_bp;
   1917  1.1    pooka 	int s;
   1918  1.2  tsutsui 	DEBUG_PRINT(("acerestart %s\n",
   1919  1.2  tsutsui 	    device_xname(ace->sc_dev)), DEBUG_XFERS);
   1920  1.1    pooka 
   1921  1.1    pooka 	s = splbio();
   1922  1.1    pooka 	__acestart(v, bp);
   1923  1.1    pooka 	splx(s);
   1924  1.1    pooka }
   1925  1.1    pooka 
   1926  1.1    pooka int
   1927  1.1    pooka aceread(dev_t dev, struct uio *uio, int flags)
   1928  1.1    pooka {
   1929  1.1    pooka 	int r;
   1930  1.1    pooka 
   1931  1.1    pooka 	DEBUG_PRINT(("aceread\n"), DEBUG_XFERS);
   1932  1.1    pooka 	r = physio(acestrategy, NULL, dev, B_READ, minphys, uio);
   1933  1.1    pooka 	DEBUG_PRINT(("aceread -> x%x resid=%x\n",r,uio->uio_resid),DEBUG_XFERS);
   1934  1.1    pooka 
   1935  1.1    pooka 	return r;
   1936  1.1    pooka }
   1937  1.1    pooka 
   1938  1.1    pooka int
   1939  1.1    pooka acewrite(dev_t dev, struct uio *uio, int flags)
   1940  1.1    pooka {
   1941  1.1    pooka 
   1942  1.1    pooka 	DEBUG_PRINT(("acewrite\n"), DEBUG_XFERS);
   1943  1.2  tsutsui 	return physio(acestrategy, NULL, dev, B_WRITE, minphys, uio);
   1944  1.1    pooka }
   1945  1.1    pooka 
   1946  1.1    pooka int
   1947  1.1    pooka aceopen(dev_t dev, int flag, int fmt, struct lwp *l)
   1948  1.1    pooka {
   1949  1.1    pooka 	struct ace_softc *ace;
   1950  1.1    pooka 	int part, error;
   1951  1.1    pooka 
   1952  1.1    pooka 	DEBUG_PRINT(("aceopen\n"), DEBUG_FUNCS);
   1953  1.1    pooka 	ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
   1954  1.1    pooka 	if (ace == NULL)
   1955  1.2  tsutsui 		return ENXIO;
   1956  1.1    pooka 
   1957  1.1    pooka 	if (! device_is_active(ace->sc_dev))
   1958  1.2  tsutsui 		return ENODEV;
   1959  1.1    pooka 
   1960  1.1    pooka 	part = ACEPART(dev);
   1961  1.1    pooka 
   1962  1.1    pooka 	mutex_enter(&ace->sc_dk.dk_openlock);
   1963  1.1    pooka 
   1964  1.1    pooka 	/*
   1965  1.1    pooka 	 * If there are wedges, and this is not RAW_PART, then we
   1966  1.1    pooka 	 * need to fail.
   1967  1.1    pooka 	 */
   1968  1.1    pooka 	if (ace->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
   1969  1.1    pooka 		error = EBUSY;
   1970  1.1    pooka 		goto bad;
   1971  1.1    pooka 	}
   1972  1.1    pooka 
   1973  1.1    pooka 	if (ace->sc_dk.dk_openmask != 0) {
   1974  1.1    pooka 		/*
   1975  1.1    pooka 		 * If any partition is open, but the disk has been invalidated,
   1976  1.1    pooka 		 * disallow further opens.
   1977  1.1    pooka 		 */
   1978  1.1    pooka 		if ((ace->sc_flags & ACEF_LOADED) == 0) {
   1979  1.1    pooka 			error = EIO;
   1980  1.1    pooka 			goto bad;
   1981  1.1    pooka 		}
   1982  1.1    pooka 	} else {
   1983  1.1    pooka 		if ((ace->sc_flags & ACEF_LOADED) == 0) {
   1984  1.1    pooka 			ace->sc_flags |= ACEF_LOADED;
   1985  1.1    pooka 
   1986  1.1    pooka 			/* Load the physical device parameters. */
   1987  1.2  tsutsui 			if (ace->sc_capacity == 0) {
   1988  1.2  tsutsui 				error = sysace_identify(ace);
   1989  1.2  tsutsui 				if (error)
   1990  1.2  tsutsui 					goto bad;
   1991  1.2  tsutsui 			}
   1992  1.1    pooka 
   1993  1.1    pooka 			/* Load the partition info if not already loaded. */
   1994  1.1    pooka 			acegetdisklabel(ace);
   1995  1.1    pooka 		}
   1996  1.1    pooka 	}
   1997  1.1    pooka 
   1998  1.1    pooka 	/* Check that the partition exists. */
   1999  1.1    pooka 	if (part != RAW_PART &&
   2000  1.1    pooka 	    (part >= ace->sc_dk.dk_label->d_npartitions ||
   2001  1.1    pooka 	     ace->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
   2002  1.1    pooka 		error = ENXIO;
   2003  1.1    pooka 		goto bad;
   2004  1.1    pooka 	}
   2005  1.1    pooka 
   2006  1.1    pooka 	/* Insure only one open at a time. */
   2007  1.1    pooka 	switch (fmt) {
   2008  1.1    pooka 	case S_IFCHR:
   2009  1.1    pooka 		ace->sc_dk.dk_copenmask |= (1 << part);
   2010  1.1    pooka 		break;
   2011  1.1    pooka 	case S_IFBLK:
   2012  1.1    pooka 		ace->sc_dk.dk_bopenmask |= (1 << part);
   2013  1.1    pooka 		break;
   2014  1.1    pooka 	}
   2015  1.1    pooka 	ace->sc_dk.dk_openmask =
   2016  1.1    pooka 	    ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
   2017  1.1    pooka 
   2018  1.1    pooka 	mutex_exit(&ace->sc_dk.dk_openlock);
   2019  1.1    pooka 	return 0;
   2020  1.1    pooka 
   2021  1.1    pooka  bad:
   2022  1.1    pooka 	mutex_exit(&ace->sc_dk.dk_openlock);
   2023  1.1    pooka 	return error;
   2024  1.1    pooka }
   2025  1.1    pooka 
   2026  1.1    pooka int
   2027  1.1    pooka aceclose(dev_t dev, int flag, int fmt, struct lwp *l)
   2028  1.1    pooka {
   2029  1.1    pooka 	struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
   2030  1.1    pooka 	int part = ACEPART(dev);
   2031  1.1    pooka 
   2032  1.1    pooka 	DEBUG_PRINT(("aceclose\n"), DEBUG_FUNCS);
   2033  1.1    pooka 	if (ace == NULL)
   2034  1.1    pooka 		return ENXIO;
   2035  1.1    pooka 
   2036  1.1    pooka 	mutex_enter(&ace->sc_dk.dk_openlock);
   2037  1.1    pooka 
   2038  1.1    pooka 	switch (fmt) {
   2039  1.1    pooka 	case S_IFCHR:
   2040  1.1    pooka 		ace->sc_dk.dk_copenmask &= ~(1 << part);
   2041  1.1    pooka 		break;
   2042  1.1    pooka 	case S_IFBLK:
   2043  1.1    pooka 		ace->sc_dk.dk_bopenmask &= ~(1 << part);
   2044  1.1    pooka 		break;
   2045  1.1    pooka 	}
   2046  1.1    pooka 	ace->sc_dk.dk_openmask =
   2047  1.1    pooka 	    ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
   2048  1.1    pooka 
   2049  1.1    pooka 	if (ace->sc_dk.dk_openmask == 0) {
   2050  1.1    pooka 
   2051  1.2  tsutsui 		if (!(ace->sc_flags & ACEF_KLABEL))
   2052  1.1    pooka 			ace->sc_flags &= ~ACEF_LOADED;
   2053  1.1    pooka 
   2054  1.1    pooka 	}
   2055  1.1    pooka 
   2056  1.1    pooka 	mutex_exit(&ace->sc_dk.dk_openlock);
   2057  1.1    pooka 	return 0;
   2058  1.1    pooka }
   2059  1.1    pooka 
   2060  1.1    pooka void
   2061  1.1    pooka acegetdefaultlabel(struct ace_softc *ace, struct disklabel *lp)
   2062  1.1    pooka {
   2063  1.1    pooka 
   2064  1.1    pooka 	DEBUG_PRINT(("acegetdefaultlabel\n"), DEBUG_FUNCS);
   2065  1.1    pooka 	memset(lp, 0, sizeof(struct disklabel));
   2066  1.1    pooka 
   2067  1.1    pooka 	lp->d_secsize = DEV_BSIZE;
   2068  1.1    pooka 	lp->d_ntracks = ace->sc_params.CurrentNumberOfHeads;
   2069  1.1    pooka 	lp->d_nsectors = ace->sc_params.CurrentSectorsPerTrack;
   2070  1.1    pooka 	lp->d_ncylinders = ace->sc_capacity /
   2071  1.2  tsutsui 	    (ace->sc_params.CurrentNumberOfHeads *
   2072  1.2  tsutsui 	     ace->sc_params.CurrentSectorsPerTrack);
   2073  1.1    pooka 	lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
   2074  1.1    pooka 
   2075  1.2  tsutsui 	lp->d_type = DTYPE_ST506; /* ?!? */
   2076  1.1    pooka 
   2077  1.1    pooka 	strncpy(lp->d_typename, ace->sc_params.ModelNumber, 16);
   2078  1.1    pooka 	strncpy(lp->d_packname, "fictitious", 16);
   2079  1.1    pooka 	if (ace->sc_capacity > UINT32_MAX)
   2080  1.1    pooka 		lp->d_secperunit = UINT32_MAX;
   2081  1.1    pooka 	else
   2082  1.1    pooka 		lp->d_secperunit = ace->sc_capacity;
   2083  1.1    pooka 	lp->d_rpm = 3600;
   2084  1.1    pooka 	lp->d_interleave = 1;
   2085  1.1    pooka 	lp->d_flags = 0;
   2086  1.1    pooka 
   2087  1.1    pooka 	lp->d_partitions[RAW_PART].p_offset = 0;
   2088  1.1    pooka 	lp->d_partitions[RAW_PART].p_size =
   2089  1.1    pooka 	    lp->d_secperunit * (lp->d_secsize / DEV_BSIZE);
   2090  1.1    pooka 	lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
   2091  1.1    pooka 	lp->d_npartitions = RAW_PART + 1;
   2092  1.1    pooka 
   2093  1.1    pooka 	lp->d_magic = DISKMAGIC;
   2094  1.1    pooka 	lp->d_magic2 = DISKMAGIC;
   2095  1.1    pooka 	lp->d_checksum = dkcksum(lp);
   2096  1.1    pooka }
   2097  1.1    pooka 
   2098  1.1    pooka /*
   2099  1.1    pooka  * Fabricate a default disk label, and try to read the correct one.
   2100  1.1    pooka  */
   2101  1.1    pooka void
   2102  1.1    pooka acegetdisklabel(struct ace_softc *ace)
   2103  1.1    pooka {
   2104  1.1    pooka 	struct disklabel *lp = ace->sc_dk.dk_label;
   2105  1.1    pooka 	const char *errstring;
   2106  1.1    pooka 
   2107  1.1    pooka 	DEBUG_PRINT(("acegetdisklabel\n"), DEBUG_FUNCS);
   2108  1.1    pooka 
   2109  1.1    pooka 	memset(ace->sc_dk.dk_cpulabel, 0, sizeof(struct cpu_disklabel));
   2110  1.1    pooka 
   2111  1.1    pooka 	acegetdefaultlabel(ace, lp);
   2112  1.1    pooka 
   2113  1.1    pooka #ifdef HAS_BAD144_HANDLING
   2114  1.1    pooka 	ace->sc_bio.badsect[0] = -1;
   2115  1.1    pooka #endif
   2116  1.1    pooka 
   2117  1.1    pooka 	errstring = readdisklabel(MAKEACEDEV(0, device_unit(ace->sc_dev),
   2118  1.1    pooka 				  RAW_PART), acestrategy, lp,
   2119  1.1    pooka 				  ace->sc_dk.dk_cpulabel);
   2120  1.1    pooka 	if (errstring) {
   2121  1.1    pooka 		printf("%s: %s\n", device_xname(ace->sc_dev), errstring);
   2122  1.1    pooka 		return;
   2123  1.1    pooka 	}
   2124  1.1    pooka 
   2125  1.1    pooka #if DEBUG
   2126  1.2  tsutsui 	if (ACE_DEBUG(DEBUG_WRITES)) {
   2127  1.2  tsutsui 		int i, n = ace->sc_dk.dk_label->d_npartitions;
   2128  1.2  tsutsui 		printf("%s: %d parts\n", device_xname(ace->sc_dev), n);
   2129  1.2  tsutsui 		for (i = 0; i < n; i++) {
   2130  1.2  tsutsui 			printf("\t[%d]: t=%x s=%d o=%d\n", i,
   2131  1.2  tsutsui 			    ace->sc_dk.dk_label->d_partitions[i].p_fstype,
   2132  1.2  tsutsui 			    ace->sc_dk.dk_label->d_partitions[i].p_size,
   2133  1.2  tsutsui 			    ace->sc_dk.dk_label->d_partitions[i].p_offset);
   2134  1.2  tsutsui 		}
   2135  1.2  tsutsui 	}
   2136  1.1    pooka #endif
   2137  1.1    pooka 
   2138  1.1    pooka #ifdef HAS_BAD144_HANDLING
   2139  1.1    pooka 	if ((lp->d_flags & D_BADSECT) != 0)
   2140  1.1    pooka 		bad144intern(ace);
   2141  1.1    pooka #endif
   2142  1.1    pooka }
   2143  1.1    pooka 
   2144  1.1    pooka void
   2145  1.1    pooka aceperror(const struct ace_softc *ace)
   2146  1.1    pooka {
   2147  1.1    pooka 	const char *devname = device_xname(ace->sc_dev);
   2148  1.2  tsutsui 	uint32_t Status = ace->sc_bio.r_error;
   2149  1.1    pooka 
   2150  1.1    pooka 	printf("%s: (", devname);
   2151  1.1    pooka 
   2152  1.1    pooka 	if (Status == 0)
   2153  1.1    pooka 		printf("error not notified");
   2154  1.2  tsutsui 	else
   2155  1.2  tsutsui 		printf("status=x%x", Status);
   2156  1.1    pooka 
   2157  1.1    pooka 	printf(")\n");
   2158  1.1    pooka }
   2159  1.1    pooka 
   2160  1.1    pooka int
   2161  1.1    pooka aceioctl(dev_t dev, u_long xfer, void *addr, int flag, struct lwp *l)
   2162  1.1    pooka {
   2163  1.1    pooka 	struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
   2164  1.1    pooka 	int error = 0, s;
   2165  1.1    pooka 
   2166  1.1    pooka 	DEBUG_PRINT(("aceioctl\n"), DEBUG_FUNCS);
   2167  1.1    pooka 
   2168  1.1    pooka 	if ((ace->sc_flags & ACEF_LOADED) == 0)
   2169  1.1    pooka 		return EIO;
   2170  1.1    pooka 
   2171  1.1    pooka 	error = disk_ioctl(&ace->sc_dk, xfer, addr, flag, l);
   2172  1.1    pooka 	if (error != EPASSTHROUGH)
   2173  1.2  tsutsui 		return error;
   2174  1.1    pooka 
   2175  1.1    pooka 	switch (xfer) {
   2176  1.1    pooka #ifdef HAS_BAD144_HANDLING
   2177  1.1    pooka 	case DIOCSBAD:
   2178  1.1    pooka 		if ((flag & FWRITE) == 0)
   2179  1.1    pooka 			return EBADF;
   2180  1.1    pooka 		ace->sc_dk.dk_cpulabel->bad = *(struct dkbad *)addr;
   2181  1.1    pooka 		ace->sc_dk.dk_label->d_flags |= D_BADSECT;
   2182  1.1    pooka 		bad144intern(ace);
   2183  1.1    pooka 		return 0;
   2184  1.1    pooka #endif
   2185  1.1    pooka 	case DIOCGDINFO:
   2186  1.1    pooka 		*(struct disklabel *)addr = *(ace->sc_dk.dk_label);
   2187  1.1    pooka 		return 0;
   2188  1.1    pooka 
   2189  1.1    pooka 	case DIOCGPART:
   2190  1.1    pooka 		((struct partinfo *)addr)->disklab = ace->sc_dk.dk_label;
   2191  1.1    pooka 		((struct partinfo *)addr)->part =
   2192  1.1    pooka 		    &ace->sc_dk.dk_label->d_partitions[ACEPART(dev)];
   2193  1.1    pooka 		return 0;
   2194  1.1    pooka 
   2195  1.1    pooka 	case DIOCWDINFO:
   2196  1.1    pooka 	case DIOCSDINFO:
   2197  1.1    pooka 	{
   2198  1.1    pooka 		struct disklabel *lp;
   2199  1.1    pooka 
   2200  1.1    pooka 		if ((flag & FWRITE) == 0)
   2201  1.1    pooka 			return EBADF;
   2202  1.1    pooka 
   2203  1.1    pooka 		lp = (struct disklabel *)addr;
   2204  1.1    pooka 
   2205  1.1    pooka 		mutex_enter(&ace->sc_dk.dk_openlock);
   2206  1.1    pooka 		ace->sc_flags |= ACEF_LABELLING;
   2207  1.1    pooka 
   2208  1.1    pooka 		error = setdisklabel(ace->sc_dk.dk_label,
   2209  1.1    pooka 		    lp, /*ace->sc_dk.dk_openmask : */0,
   2210  1.1    pooka 		    ace->sc_dk.dk_cpulabel);
   2211  1.1    pooka 		if (error == 0) {
   2212  1.1    pooka 			if (xfer == DIOCWDINFO)
   2213  1.1    pooka 				error = writedisklabel(ACELABELDEV(dev),
   2214  1.1    pooka 				    acestrategy, ace->sc_dk.dk_label,
   2215  1.1    pooka 				    ace->sc_dk.dk_cpulabel);
   2216  1.1    pooka 		}
   2217  1.1    pooka 
   2218  1.1    pooka 		ace->sc_flags &= ~ACEF_LABELLING;
   2219  1.1    pooka 		mutex_exit(&ace->sc_dk.dk_openlock);
   2220  1.1    pooka 		return error;
   2221  1.1    pooka 	}
   2222  1.1    pooka 
   2223  1.1    pooka 	case DIOCKLABEL:
   2224  1.1    pooka 		if (*(int *)addr)
   2225  1.1    pooka 			ace->sc_flags |= ACEF_KLABEL;
   2226  1.1    pooka 		else
   2227  1.1    pooka 			ace->sc_flags &= ~ACEF_KLABEL;
   2228  1.1    pooka 		return 0;
   2229  1.1    pooka 
   2230  1.1    pooka 	case DIOCWLABEL:
   2231  1.1    pooka 		if ((flag & FWRITE) == 0)
   2232  1.1    pooka 			return EBADF;
   2233  1.1    pooka 		if (*(int *)addr)
   2234  1.1    pooka 			ace->sc_flags |= ACEF_WLABEL;
   2235  1.1    pooka 		else
   2236  1.1    pooka 			ace->sc_flags &= ~ACEF_WLABEL;
   2237  1.1    pooka 		return 0;
   2238  1.1    pooka 
   2239  1.1    pooka 	case DIOCGDEFLABEL:
   2240  1.1    pooka 		acegetdefaultlabel(ace, (struct disklabel *)addr);
   2241  1.1    pooka 		return 0;
   2242  1.1    pooka 
   2243  1.1    pooka 	case DIOCCACHESYNC:
   2244  1.1    pooka 		return 0;
   2245  1.1    pooka 
   2246  1.1    pooka 	case DIOCAWEDGE:
   2247  1.1    pooka 	    {
   2248  1.2  tsutsui 		struct dkwedge_info *dkw = (void *) addr;
   2249  1.1    pooka 
   2250  1.1    pooka 		if ((flag & FWRITE) == 0)
   2251  1.2  tsutsui 			return EBADF;
   2252  1.1    pooka 
   2253  1.1    pooka 		/* If the ioctl happens here, the parent is us. */
   2254  1.1    pooka 		strcpy(dkw->dkw_parent, device_xname(ace->sc_dev));
   2255  1.2  tsutsui 		return dkwedge_add(dkw);
   2256  1.1    pooka 	    }
   2257  1.1    pooka 
   2258  1.1    pooka 	case DIOCDWEDGE:
   2259  1.1    pooka 	    {
   2260  1.2  tsutsui 		struct dkwedge_info *dkw = (void *) addr;
   2261  1.1    pooka 
   2262  1.1    pooka 		if ((flag & FWRITE) == 0)
   2263  1.2  tsutsui 			return EBADF;
   2264  1.1    pooka 
   2265  1.1    pooka 		/* If the ioctl happens here, the parent is us. */
   2266  1.1    pooka 		strcpy(dkw->dkw_parent, device_xname(ace->sc_dev));
   2267  1.2  tsutsui 		return dkwedge_del(dkw);
   2268  1.1    pooka 	    }
   2269  1.1    pooka 
   2270  1.1    pooka 	case DIOCLWEDGES:
   2271  1.1    pooka 	    {
   2272  1.2  tsutsui 		struct dkwedge_list *dkwl = (void *) addr;
   2273  1.1    pooka 
   2274  1.2  tsutsui 		return dkwedge_list(&ace->sc_dk, dkwl, l);
   2275  1.1    pooka 	    }
   2276  1.1    pooka 
   2277  1.1    pooka 	case DIOCGSTRATEGY:
   2278  1.1    pooka 	    {
   2279  1.1    pooka 		struct disk_strategy *dks = (void *)addr;
   2280  1.1    pooka 
   2281  1.1    pooka 		s = splbio();
   2282  1.1    pooka 		strlcpy(dks->dks_name, bufq_getstrategyname(ace->sc_q),
   2283  1.1    pooka 		    sizeof(dks->dks_name));
   2284  1.1    pooka 		splx(s);
   2285  1.1    pooka 		dks->dks_paramlen = 0;
   2286  1.1    pooka 
   2287  1.1    pooka 		return 0;
   2288  1.1    pooka 	    }
   2289  1.2  tsutsui 
   2290  1.1    pooka 	case DIOCSSTRATEGY:
   2291  1.1    pooka 	    {
   2292  1.1    pooka 		struct disk_strategy *dks = (void *)addr;
   2293  1.1    pooka 		struct bufq_state *new;
   2294  1.1    pooka 		struct bufq_state *old;
   2295  1.1    pooka 
   2296  1.1    pooka 		if ((flag & FWRITE) == 0) {
   2297  1.1    pooka 			return EBADF;
   2298  1.1    pooka 		}
   2299  1.1    pooka 		if (dks->dks_param != NULL) {
   2300  1.1    pooka 			return EINVAL;
   2301  1.1    pooka 		}
   2302  1.1    pooka 		dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
   2303  1.1    pooka 		error = bufq_alloc(&new, dks->dks_name,
   2304  1.1    pooka 		    BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
   2305  1.1    pooka 		if (error) {
   2306  1.1    pooka 			return error;
   2307  1.1    pooka 		}
   2308  1.1    pooka 		s = splbio();
   2309  1.1    pooka 		old = ace->sc_q;
   2310  1.1    pooka 		bufq_move(new, old);
   2311  1.1    pooka 		ace->sc_q = new;
   2312  1.1    pooka 		splx(s);
   2313  1.1    pooka 		bufq_free(old);
   2314  1.1    pooka 
   2315  1.1    pooka 		return 0;
   2316  1.1    pooka 	    }
   2317  1.1    pooka 
   2318  1.1    pooka #ifdef USE_ACE_FOR_RECONFIG
   2319  1.2  tsutsui 	/*
   2320  1.2  tsutsui 	 * Ok, how do I get this standardized
   2321  1.2  tsutsui 	 * [nothing to do with disks either]
   2322  1.2  tsutsui 	 */
   2323  1.1    pooka #define DIOC_FPGA_RECONFIGURE _IOW('d',166, struct ioctl_pt)
   2324  1.1    pooka 	case DIOC_FPGA_RECONFIGURE:
   2325  1.1    pooka 	    {
   2326  1.2  tsutsui 		/*
   2327  1.2  tsutsui 		 * BUGBUG This is totally wrong, we need to fault in
   2328  1.2  tsutsui 		 * all data in advance.
   2329  1.2  tsutsui 		 * Otherwise we get back here with the sysace in a bad state
   2330  1.2  tsutsui 		 * (its NOT reentrant!)
   2331  1.2  tsutsui 		 */
   2332  1.2  tsutsui 		struct ioctl_pt *pt = (struct ioctl_pt *)addr;
   2333  1.2  tsutsui 		return sysace_send_config(ace,(uint32_t*)pt->data,pt->com);
   2334  1.1    pooka 	    }
   2335  1.1    pooka #endif /* USE_ACE_FOR_RECONFIG */
   2336  1.1    pooka 
   2337  1.1    pooka 	default:
   2338  1.2  tsutsui 		/*
   2339  1.2  tsutsui 		 * NB: we get a DIOCGWEDGEINFO, but nobody else handles it
   2340  1.2  tsutsui 		 * either
   2341  1.2  tsutsui 		 */
   2342  1.2  tsutsui 		DEBUG_PRINT(("aceioctl: unsup x%lx\n", xfer), DEBUG_FUNCS);
   2343  1.1    pooka 		return ENOTTY;
   2344  1.1    pooka 	}
   2345  1.1    pooka }
   2346  1.1    pooka 
   2347  1.1    pooka int
   2348  1.1    pooka acesize(dev_t dev)
   2349  1.1    pooka {
   2350  1.1    pooka 	struct ace_softc *ace;
   2351  1.1    pooka 	int part, omask;
   2352  1.1    pooka 	int size;
   2353  1.1    pooka 
   2354  1.1    pooka 	DEBUG_PRINT(("acesize\n"), DEBUG_FUNCS);
   2355  1.1    pooka 
   2356  1.1    pooka 	ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
   2357  1.1    pooka 	if (ace == NULL)
   2358  1.2  tsutsui 		return -1;
   2359  1.1    pooka 
   2360  1.1    pooka 	part = ACEPART(dev);
   2361  1.1    pooka 	omask = ace->sc_dk.dk_openmask & (1 << part);
   2362  1.1    pooka 
   2363  1.1    pooka 	if (omask == 0 && aceopen(dev, 0, S_IFBLK, NULL) != 0)
   2364  1.2  tsutsui 		return -1;
   2365  1.1    pooka 	if (ace->sc_dk.dk_label->d_partitions[part].p_fstype != FS_SWAP)
   2366  1.1    pooka 		size = -1;
   2367  1.1    pooka 	else
   2368  1.1    pooka 		size = ace->sc_dk.dk_label->d_partitions[part].p_size *
   2369  1.1    pooka 		    (ace->sc_dk.dk_label->d_secsize / DEV_BSIZE);
   2370  1.1    pooka 	if (omask == 0 && aceclose(dev, 0, S_IFBLK, NULL) != 0)
   2371  1.2  tsutsui 		return -1;
   2372  1.2  tsutsui 	return size;
   2373  1.1    pooka }
   2374  1.1    pooka 
   2375  1.1    pooka /* #define ACE_DUMP_NOT_TRUSTED if you just want to watch */
   2376  1.1    pooka #define ACE_DUMP_NOT_TRUSTED
   2377  1.1    pooka static int acedoingadump = 0;
   2378  1.1    pooka 
   2379  1.1    pooka /*
   2380  1.1    pooka  * Dump core after a system crash.
   2381  1.1    pooka  */
   2382  1.1    pooka int
   2383  1.1    pooka acedump(dev_t dev, daddr_t blkno, void *va, size_t size)
   2384  1.1    pooka {
   2385  1.1    pooka 	struct ace_softc *ace;	/* disk unit to do the I/O */
   2386  1.1    pooka 	struct disklabel *lp;   /* disk's disklabel */
   2387  1.1    pooka 	int part, err;
   2388  1.1    pooka 	int nblks;	/* total number of sectors left to write */
   2389  1.1    pooka 
   2390  1.1    pooka 	/* Check if recursive dump; if so, punt. */
   2391  1.1    pooka 	if (acedoingadump)
   2392  1.1    pooka 		return EFAULT;
   2393  1.1    pooka 	acedoingadump = 1;
   2394  1.1    pooka 
   2395  1.1    pooka 	ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
   2396  1.1    pooka 	if (ace == NULL)
   2397  1.2  tsutsui 		return ENXIO;
   2398  1.1    pooka 
   2399  1.1    pooka 	part = ACEPART(dev);
   2400  1.1    pooka 
   2401  1.1    pooka 	/* Convert to disk sectors.  Request must be a multiple of size. */
   2402  1.1    pooka 	lp = ace->sc_dk.dk_label;
   2403  1.1    pooka 	if ((size % lp->d_secsize) != 0)
   2404  1.1    pooka 		return EFAULT;
   2405  1.1    pooka 	nblks = size / lp->d_secsize;
   2406  1.1    pooka 	blkno = blkno / (lp->d_secsize / DEV_BSIZE);
   2407  1.1    pooka 
   2408  1.1    pooka 	/* Check transfer bounds against partition size. */
   2409  1.1    pooka 	if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
   2410  1.1    pooka 		return EINVAL;
   2411  1.1    pooka 
   2412  1.1    pooka 	/* Offset block number to start of partition. */
   2413  1.1    pooka 	blkno += lp->d_partitions[part].p_offset;
   2414  1.1    pooka 
   2415  1.1    pooka 	ace->sc_bp = NULL;
   2416  1.1    pooka 	ace->sc_bio.blkno = blkno;
   2417  1.1    pooka 	ace->sc_bio.flags = ATA_POLL;
   2418  1.1    pooka 	ace->sc_bio.nbytes = nblks * lp->d_secsize;
   2419  1.1    pooka 	ace->sc_bio.databuf = va;
   2420  1.1    pooka #ifndef ACE_DUMP_NOT_TRUSTED
   2421  1.2  tsutsui 	ace->active_xfer = bp;
   2422  1.2  tsutsui 	wakeup(&ace->ch_thread);
   2423  1.2  tsutsui 
   2424  1.1    pooka 	switch(ace->sc_bio.error) {
   2425  1.1    pooka 	case ETIMEDOUT:
   2426  1.1    pooka 		printf("acedump: device timed out");
   2427  1.1    pooka 		err = EIO;
   2428  1.1    pooka 		break;
   2429  1.1    pooka 	case 0:
   2430  1.1    pooka 		err = 0;
   2431  1.1    pooka 		break;
   2432  1.1    pooka 	default:
   2433  1.1    pooka 		panic("acedump: unknown error type");
   2434  1.1    pooka 	}
   2435  1.1    pooka 	if (err != 0) {
   2436  1.1    pooka 		printf("\n");
   2437  1.1    pooka 		return err;
   2438  1.1    pooka 	}
   2439  1.1    pooka #else	/* ACE_DUMP_NOT_TRUSTED */
   2440  1.1    pooka 	/* Let's just talk about this first... */
   2441  1.1    pooka 	device_printf(ace->sc_dev, ": dump addr 0x%p, size %zu blkno %llx\n",
   2442  1.1    pooka 	    va, size, blkno);
   2443  1.1    pooka 	DELAY(500 * 1000);	/* half a second */
   2444  1.2  tsutsui 	err = 0;
   2445  1.1    pooka #endif
   2446  1.1    pooka 
   2447  1.1    pooka 	acedoingadump = 0;
   2448  1.1    pooka 	return 0;
   2449  1.1    pooka }
   2450  1.1    pooka 
   2451  1.1    pooka #ifdef HAS_BAD144_HANDLING
   2452  1.1    pooka /*
   2453  1.1    pooka  * Internalize the bad sector table.
   2454  1.1    pooka  */
   2455  1.1    pooka void
   2456  1.1    pooka bad144intern(struct ace_softc *ace)
   2457  1.1    pooka {
   2458  1.1    pooka 	struct dkbad *bt = &ace->sc_dk.dk_cpulabel->bad;
   2459  1.1    pooka 	struct disklabel *lp = ace->sc_dk.dk_label;
   2460  1.1    pooka 	int i = 0;
   2461  1.1    pooka 
   2462  1.1    pooka 	DEBUG_PRINT(("bad144intern\n"), DEBUG_XFERS);
   2463  1.1    pooka 
   2464  1.1    pooka 	for (; i < NBT_BAD; i++) {
   2465  1.1    pooka 		if (bt->bt_bad[i].bt_cyl == 0xffff)
   2466  1.1    pooka 			break;
   2467  1.1    pooka 		ace->sc_bio.badsect[i] =
   2468  1.1    pooka 		    bt->bt_bad[i].bt_cyl * lp->d_secpercyl +
   2469  1.1    pooka 		    (bt->bt_bad[i].bt_trksec >> 8) * lp->d_nsectors +
   2470  1.1    pooka 		    (bt->bt_bad[i].bt_trksec & 0xff);
   2471  1.1    pooka 	}
   2472  1.1    pooka 	for (; i < NBT_BAD+1; i++)
   2473  1.1    pooka 		ace->sc_bio.badsect[i] = -1;
   2474  1.1    pooka }
   2475  1.1    pooka #endif
   2476  1.1    pooka 
   2477  1.1    pooka static void
   2478  1.1    pooka ace_params_to_properties(struct ace_softc *ace)
   2479  1.1    pooka {
   2480  1.1    pooka 	prop_dictionary_t disk_info, odisk_info, geom;
   2481  1.1    pooka 	const char *cp;
   2482  1.1    pooka 
   2483  1.1    pooka 	disk_info = prop_dictionary_create();
   2484  1.1    pooka 
   2485  1.2  tsutsui 	cp = ST506;
   2486  1.1    pooka 
   2487  1.1    pooka 	prop_dictionary_set_cstring_nocopy(disk_info, "type", cp);
   2488  1.1    pooka 
   2489  1.1    pooka 	geom = prop_dictionary_create();
   2490  1.1    pooka 
   2491  1.1    pooka 	prop_dictionary_set_uint64(geom, "sectors-per-unit", ace->sc_capacity);
   2492  1.1    pooka 
   2493  1.1    pooka 	prop_dictionary_set_uint32(geom, "sector-size",
   2494  1.2  tsutsui 	    DEV_BSIZE /* XXX 512? */);
   2495  1.1    pooka 
   2496  1.1    pooka 	prop_dictionary_set_uint16(geom, "sectors-per-track",
   2497  1.2  tsutsui 	    ace->sc_params.CurrentSectorsPerTrack);
   2498  1.1    pooka 
   2499  1.1    pooka 	prop_dictionary_set_uint16(geom, "tracks-per-cylinder",
   2500  1.2  tsutsui 	    ace->sc_params.CurrentNumberOfHeads);
   2501  1.1    pooka 
   2502  1.2  tsutsui 	prop_dictionary_set_uint64(geom, "cylinders-per-unit",
   2503  1.2  tsutsui 	    ace->sc_capacity /
   2504  1.2  tsutsui 	    (ace->sc_params.CurrentNumberOfHeads *
   2505  1.2  tsutsui 	     ace->sc_params.CurrentSectorsPerTrack));
   2506  1.1    pooka 
   2507  1.1    pooka 	prop_dictionary_set(disk_info, "geometry", geom);
   2508  1.1    pooka 	prop_object_release(geom);
   2509  1.1    pooka 
   2510  1.1    pooka 	prop_dictionary_set(device_properties(ace->sc_dev),
   2511  1.2  tsutsui 	    "disk-info", disk_info);
   2512  1.1    pooka 
   2513  1.1    pooka 	/*
   2514  1.1    pooka 	 * Don't release disk_info here; we keep a reference to it.
   2515  1.1    pooka 	 * disk_detach() will release it when we go away.
   2516  1.1    pooka 	 */
   2517  1.1    pooka 
   2518  1.1    pooka 	odisk_info = ace->sc_dk.dk_info;
   2519  1.1    pooka 	ace->sc_dk.dk_info = disk_info;
   2520  1.1    pooka 	if (odisk_info)
   2521  1.1    pooka 		prop_object_release(odisk_info);
   2522  1.1    pooka }
   2523