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