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