/*-
* Copyright (c) 2010 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code was written by Alessandro Forin and Neil Pittman
* at Microsoft Research and contributed to The NetBSD Foundation
* by Microsoft Corporation.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/* Structure returned by the Identify command (see CFlash specs)
* NB: We only care for the first sector so that is what we define here.
* NB: Beware of mis-alignment for all 32bit things
*/
typedef struct _CFLASH_IDENTIFY {
uint16_t Signature; /* Word 0 */
#define CFLASH_SIGNATURE 0x848a
uint16_t DefaultNumberOfCylinders; /* Word 1 */
uint16_t Reserved1; /* Word 2 */
uint16_t DefaultNumberOfHeads; /* Word 3 */
uint16_t Obsolete1[2]; /* Word 4 */
uint16_t DefaultSectorsPerTrack; /* Word 6 */
uint16_t SectorsPerCard[2]; /* Word 7 */
uint16_t Obsolete2; /* Word 9 */
uint8_t SerialNumber[20]; /* padded, right-justified Word 10 */
uint16_t Obsolete3[2]; /* Word 20 */
uint16_t EccBytesInRWLong; /* Word 22 */
uint8_t FirmwareRevision[8]; /* Word 23 */
uint8_t ModelNumber[40]; /* Word 27 */
uint16_t SectorsInRWMultiple; /* Word 47 */
uint16_t Reserved2; /* Word 48 */
uint16_t Capabilities; /* Word 49 */
uint16_t Reserved3; /* Word 50 */
uint16_t PioMode; /* Word 51 */
uint16_t Obsolete4; /* Word 52 */
uint16_t FieldValidity; /* Word 53 */
uint16_t CurrentNumberOfCylinders; /* Word 54 */
uint16_t CurrentNumberOfHeads; /* Word 55 */
uint16_t CurrentSectorsPerTrack; /* Word 56 */
uint16_t CurrentCapacity[2]; /* Word 57 */
uint16_t MultiSectorSettings; /* Word 59 */
uint16_t NumberOfAddressableSectors[2]; /* Word 60 */
uint16_t Reserved4; /* Word 62 */
uint16_t MultiWordDmaTransfer; /* Word 63 */
uint16_t AdvancedPioModes; /* Word 64 */
uint16_t MinimumMultiWordDmaTiming; /* Word 65 */
uint16_t RecommendedMultiWordDmaTiming; /* Word 66 */
uint16_t PioTimingNoFlowControl; /* Word 67 */
uint16_t PioTimingWithFlowControl; /* Word 68 */
uint16_t Reserved5[13]; /* Word 69 */
uint16_t FeaturesSupported[3]; /* Word 82 */
uint16_t FeaturesEnabled[3]; /* Word 85 */
uint16_t UdmaMode; /* Word 88 */
uint16_t SecurityEraseTime; /* Word 89 */
uint16_t EnhancedSecurityEraseTime; /* Word 90 */
uint16_t CurrentPowerManagementValue; /* Word 91 */
uint8_t Reserved6[72]; /* Word 92-127 */
uint8_t SecondHalf[256]; /* Word 128-255 */
} CFLASH_IDENTIFY, *PCFLASH_IDENTIFY;
#define SIZEOF_IDENTIFY CF_SECTOR_SIZE /* must be a sector multiple */
/* Instead of dragging in atavar.h.. */
/*
* Parameters/state needed by the controller to perform an ATA bio.
*/
struct ace_bio {
volatile int flags;/* cmd flags */
#define ATA_POLL 0x0002 /* poll for completion */
#define ATA_SINGLE 0x0008 /* transfer must be done in singlesector mode */
#define ATA_READ 0x0020 /* transfer is a read (otherwise a write) */
#define ATA_CORR 0x0040 /* transfer had a corrected error */
daddr_t blkno; /* block addr */
daddr_t blkdone;/* number of blks transferred */
size_t nblks; /* number of blocks currently transferring */
size_t nbytes; /* number of bytes currently transferring */
char *databuf;/* data buffer address */
volatile int error;
#define NOERROR 0 /* There was no error (r_error invalid),
else see acedone()*/
#define FAILED(er) (er != 0)
#define EDOOFUS EIO
uint32_t r_error;/* copy of status register */
#ifdef HAS_BAD144_HANDLING
daddr_t badsect[127];/* 126 plus trailing -1 marker */
#endif
};
/* End of atavar.h*/
struct ace_softc {
/* General disk infos */
device_t sc_dev;
struct disk sc_dk;
struct bufq_state *sc_q;
struct callout sc_restart_ch;
/* IDE disk soft states */
struct buf *sc_bp; /* buf being transferred */
struct buf *active_xfer; /* buf handoff to thread */
/* current transfer data */
struct ace_bio sc_bio; /* current transfer */
struct proc *ch_thread;
int ch_flags;
#define ATACH_SHUTDOWN 0x02 /* thread is shutting down */
#define ATACH_IRQ_WAIT 0x10 /* thread is waiting for irq */
#define ATACH_DISABLED 0x80 /* channel is disabled */
#define ATACH_TH_RUN 0x100 /* the kernel thread is working */
#define ATACH_TH_RESET 0x200 /* someone ask the thread to reset */
int openings;
int media_has_changed;
#define ACECE_MC 0x20 /* media changed */
#define ACECE_MCR 0x08 /* media change requested */
struct _CFLASH_IDENTIFY sc_params;/* drive characteristics found */
int sc_flags;
#define ACEF_WLABEL 0x004 /* label is writable */
#define ACEF_LABELLING 0x008 /* writing label */
#define ACEF_LOADED 0x010 /* parameters loaded */
#define ACEF_WAIT 0x020 /* waiting for resources */
#define ACEF_KLABEL 0x080 /* retain label after 'full' close */
uint64_t sc_capacity;
uint32_t sc_multi; /* max sectors per xfer */
struct _Sac *sc_dr; /* reg pointers */
int hw_busy;
int retries; /* number of xfer retry */
krndsource_t rnd_source;
};
int ace_ebus_match(device_t, cfdata_t, void *);
void ace_ebus_attach(device_t, device_t, void *);
void aceattach(struct ace_softc *);
int acedetach(device_t, int);
int aceactivate(device_t, enum devact);
/*
* Spin a bit until we get it
*/
for (i = 0; i < 200; i++) {
Status = sc->sc_dr->STATUS;
if (Status & SAC_MPULOCK)
return TRUE;
DELAY(100);
DBGME(DEBUG_FUNCS,
printf("Sysace::Lock loops.. (st=%x)\n",Status));
}
/*
* Spin a bit until we get it
*/
for (i = 0; i < 200; i++) {
Status = sc->sc_dr->STATUS;
if ((Status & SAC_MPULOCK) == 0)
return TRUE;
DELAY(100);
DBGME(DEBUG_FUNCS,
printf("Sysace::Unlock loops.. (st=%x)\n",Status));
}
/*
* Check if the ACE is waiting for a comamnd
*/
#define sysace_ready(_s_) ((_s_)->sc_dr->STATUS & SAC_RDYFORCFCMD)
/*
* Check if the ACE is executing a comamnd
*/
#define sysace_busy(_s_) ((_s_)->sc_dr->STATUS & SAC_CFBSY)
/*
* Turn on interrupts from the ACE
*/
#define sysace_inton(_s_) { \
(_s_)->sc_dr->CONTROLREG |= SAC_INTERRUPTS; \
(_s_)->sc_dr->Control |= SAC_INTMASK; \
}
/*
* Turn off interrupts from the ACE
*/
#define sysace_intoff(_s_) { \
(_s_)->sc_dr->CONTROLREG &= ~SAC_INTERRUPTS; \
(_s_)->sc_dr->Control &= ~SAC_INTMASK; \
}
/*
* Start a command on the ACE, such as read or identify.
*/
static int
sysace_start(struct ace_softc *sc, uint32_t Command, uint32_t Lba,
uint32_t nSectors)
{
/*
* Lock it if not already
*/
if (!sysace_lock_registers(sc)) {
/* printed already */
return ETIMEDOUT;
}
/*
* Is there a CF inserted
*/
if (!(sc->sc_dr->STATUS & SAC_CFDETECT)) {
/* NB: Not a failure state */
DBGME(DEBUG_ERRORS,
printf("Sysace:: no media (st=%x)\n", sc->sc_dr->STATUS));
if (sc->sc_capacity) {
sc->media_has_changed = TRUE;
sc->sc_capacity = 0;
}
return ENODEV;
}
/*
* Is it ready for a command
*/
if (!sysace_ready(sc)) {
DBGME(DEBUG_ERRORS,
printf("Sysace:: not ready (st=%x)\n", sc->sc_dr->STATUS));
SysaceDumpRegisters(sc->sc_dr);
return EBUSY;
}
/*
* sector number and command
*/
sc->sc_dr->MPULBAREG = Lba;
sc->sc_dr->SECCNTCMDREG =
(uint16_t)(Command | (nSectors & SAC_SECCCNT));
/*
* re-route the chip
* NB: The "RESET" is actually not much of a misnomer.
* The chip was designed for a one-shot execution at reset time,
* namely loading the configuration data into the FPGA. So.
*/
sc->hw_busy = TRUE;
sc->sc_dr->CONTROLREG |= SAC_CFGRESET;
return 0;
}
/*
* Identify the (size of the) CompactFlash card inserted in the slot.
*/
static int
sysace_identify(struct ace_softc *sc)
{
PCFLASH_IDENTIFY Identify = &sc->sc_params;
uint32_t Status = 0;
int i, j, error;
/*
* Turn on interrupts before we start the command
*/
sysace_inton(sc); /* BUGBUG we should add polling mode (for dump too) */
/*
* This will invalidate the ACE's current sector data
*/
sc->sc_capacity = 0;
/*
* Get it going
*/
error = sysace_start(sc, SAC_CMD_IDENTIFYMEMCARD, 0, 1);
/*
* Wait until its done
*/
if (!FAILED(error)) {
/* Might be called during autoconf, no interrupts */
if (cold) {
do {
DELAY(10);
Status = sc->sc_dr->STATUS;
} while ((Status &
(SAC_DATABUFRDY|SAC_CFCERROR|SAC_CFGERROR)) == 0);
} else {
while (sc->hw_busy) {
DBGME(DEBUG_FUNCS,
printf("Sysace:: cwait.. (st=%x)"
" sizeof=%d\n",
sc->sc_dr->STATUS, sizeof(*Identify)));
error = tsleep(&sc->media_has_changed, PRIBIO,
"aceidfy", 0);
}
}
/*
* Did it work?
*/
Status = sc->sc_dr->STATUS;
if (Status & SAC_DATABUFRDY) {
/*
* Yes, pull out all the data.
* NB: Until we do so the chip will not be ready for
* another command
*/
for (i = 0; i < sizeof(*Identify); i += 4) {
/*
* Verify the (32-bytes) FIFO has reloaded
*/
for (j = 0; j < 10; j++) {
Status = sc->sc_dr->STATUS;
if (Status & SAC_DATABUFRDY)
break;
DELAY(10);
}
if (Status & SAC_DATABUFRDY) {
uint32_t Data32;
/*
* This pulls two 16-bit words out of
* the FIFO.
* They are ordered in LE.
* NB: Yes this is different from
* regular data accesses
*/
Data32 = sc->sc_dr->DATABUFREG[0];
#if _BYTE_ORDER == _LITTLE_ENDIAN
/* all is fine */
#else
Data32 =
(Data32 >> 16) | (Data32 << 16);
#endif
memcpy(((char *)Identify) + i,
&Data32, 4);
} else {
/*
* Ooops, what's going on here?
*/
DBGME(DEBUG_ERRORS,
printf("Sysace::!DATABUFRDY %x\n",
Status));
error = EIO;
break;
}
}
/*
* Make sure we did ok and pick up the relevant info
*/
if (Status & SAC_DATABUFRDY) {
DBGME(DEBUG_XFERS,
device_printf(sc->sc_dev,
"model: %.40s/%.20s\n",
Identify->ModelNumber,
Identify->SerialNumber));
if (Identify->Signature == CFLASH_SIGNATURE) {
DBGME(DEBUG_PROBE,
printf("Sysace::Card is"
" %.40s::%.20s\n",
Identify->ModelNumber,
Identify->SerialNumber));
sc->sc_capacity =
(Identify->SectorsPerCard[0] << 16)
| Identify->SectorsPerCard[1];
DBGME(DEBUG_PROBE,
printf("Sysace::sc_capacity x%qx\n",
sc->sc_capacity));
ace_set_geometry(sc);
} else {
DBGME(DEBUG_ERRORS,
printf("Sysace::Bad card signature?"
" %x != %x\n",
Identify->Signature,
CFLASH_SIGNATURE));
sc->sc_capacity = 0;
error = ENXIO;
}
} else {
error = ETIMEDOUT;
}
} else {
/*
* No, it did not work. Maybe there is no card inserted
*/
DBGME(DEBUG_ERRORS,
printf("Sysace::Identify failed,"
" missing CFLASH card?\n"));
SysaceDumpRegisters(sc->sc_dr);
/* BUGBUG Fix the error code accordingly */
error = ETIMEDOUT;
}
}
/* remember this jic */
sc->sc_bio.r_error = Status;
/* Free the ACE for the JTAG, just in case */
sysace_unlock_registers(sc);
/*
* Done
*/
return error;
}
/*
* Common code for read&write argument validation
*/
static int
sysace_validate(struct ace_softc *sc, daddr_t start, size_t *pSize)
{
daddr_t Size;
/*
* Verify that we know the media size
*/
if (sc->sc_capacity == 0) {
int error = sysace_identify(sc);
if (FAILED(error))
return error;
}
/* Read SIZE bytes from sysace device, at offset Position
*/
uint32_t ace_maxatatime = 255;
#define MAXATATIME ace_maxatatime //255 /* BUGBUG test me on real hardware!! */
static int
sysace_read_at(struct ace_softc *sc, daddr_t start_sector, char *buffer,
size_t nblocks, size_t *pSizeRead)
{
int error;
uint32_t BlocksThisTime;
uint32_t Status = 0, SizeRead = 0;
uint32_t i, j;
/*
* Repeat until we are done or error
*/
while (error == 0) {
/*
* .. one sector at a time
* BUGBUG Supposedly we can do up to 256 sectors?
*/
BlocksThisTime = nblocks;
if (BlocksThisTime > MAXATATIME)
BlocksThisTime = MAXATATIME;
/*
* Yes, start a sector write
*/
sysace_inton(sc);
error = sysace_start(sc,
SAC_CMD_WRITEMEMCARDDATA,
(uint32_t)start_sector, /* BUGBUG trims here, no warn. */
BlocksThisTime);
/*
* And wait until done, if ok
*/
if (!FAILED(error)) {
start_sector += BlocksThisTime;
/* BUGBUG timeouts! */
while (sc->hw_busy) {
error = tsleep(&sc->media_has_changed,
PRIBIO, "acewrite", 0);
}
}
/*
* Are we doing ok
*/
if (!FAILED(error)) {
/*
* Get the data out to the ACE
*/
for (i = 0; i < (BlocksThisTime << CF_SECBITS);
i += 4) {
/*
* Make sure the FIFO is ready
*/
for (j = 0; j < 10; j++) {
Status = sc->sc_dr->STATUS;
if (Status & SAC_DATABUFRDY)
break;
DELAY(1000);
}
/*
* We need to wait until the device is ready for the
* next command
* Experimentation shows that it can take longer than 10msec.
*/
if (!FAILED(error)) {
for (j = 0; j < 300; j++) {
Status = sc->sc_dr->STATUS;
if (Status & SAC_RDYFORCFCMD)
break;
(void)tsleep(&sc->media_has_changed,
PRIBIO, "acewrite", 2);
}
if (!(Status & SAC_RDYFORCFCMD)) {
DBGME(DEBUG_ERRORS,
printf("Sysace::WRITE-COMPLETE timeout"
" St=%x\n", Status));
SysaceDumpRegisters(sc->sc_dr);
/*
* Ignore, we'll handle it the next time around
* BUGBUG To be revised along with non-existent
* error handling
*/
}
}
/* Free the ACE for the JTAG, just in case */
sysace_unlock_registers(sc);
/*
* Are we done yet?
*/
if (nblocks == 0)
break;
}
if (pSizeWritten)
*pSizeWritten = SizeWritten;
return error;
}
#ifdef USE_ACE_FOR_RECONFIG
static int
sysace_send_config(struct ace_softc *sc, uint32_t *Data, unsigned int nBytes)
{
struct _Sac *Interface = sc->sc_dr;
unsigned int i, j, nWords;
uint32_t CtlWas;
uint32_t Status;
CtlWas = Interface->CONTROLREG;
/* Set the bits but in RESET (pag 49-50 of specs)*/
#define CFGCMD (SAC_FORCELOCKREQ | SAC_LOCKREQ | SAC_CFGSEL | \
SAC_FORCECFGMODE |/* SAC_CFGMODE |*/ SAC_CFGSTART)
Interface->CONTROLREG = CFGCMD | SAC_CFGRESET;
/* Take it out of RESET */
Interface->CONTROLREG = CFGCMD;
/*
* Must wait till it says READY
* It can take a looong time
*/
for (j = 0; j < 1000; j++) {
Status = Interface->STATUS;
if (Status & SAC_RDYFORCFCMD)
break;
DELAY(1000);
}
/*
* Get the data out to the ACE
*/
#define ACEROUNDUP 32
nBytes = (nBytes + ACEROUNDUP - 1) & ~(ACEROUNDUP-1);
nWords = (nBytes + 3) / 4;
DBGME(DEBUG_FUNCS,
printf("Sending %d bytes (as %d words) to %p ",
nBytes, nWords, Interface));
for (i = 0; i < nWords; i += 1/*word*/) {
/* Stop on errors */
Status = Interface->ERRORREG;
if (Status) {
/*
* Ooops, get out of here
*/
DBGME(DEBUG_ERRORS,
printf("Sysace::CFG error %x (i=%d)\n", Status, i));
goto Error;
}
/*
* Make sure the FIFO is ready
*/
for (j = 0; j < 100; j++) {
Status = Interface->STATUS;
if (Status & SAC_DATABUFRDY)
break;
DELAY(1000);
}
/*
* Rest of code lifted with mods from the dev\ata\wd.c driver
*/
/*
* Copyright (c) 1998, 2001 Manuel Bouyer. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*-
* Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Charles M. Hannum and by Onno van der Linden.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
static const char ST506[] = "ST506";
#define ACEIORETRIES_SINGLE 4 /* number of retries before single-sector */
#define ACEIORETRIES 5 /* number of retries before giving up */
#define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
#define ACEUNIT(dev) DISKUNIT(dev)
#define ACEPART(dev) DISKPART(dev)
#define ACEMINOR(unit, part) DISKMINOR(unit, part)
#define MAKEACEDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
/* setup all required fields so that if the attach fails we are ok */
ace->sc_dk.dk_driver = &acedkdriver;
ace->sc_dk.dk_name = device_xname(ace->sc_dev);
/* read our drive info */
if (sysace_attach(ace) != 0) {
aprint_error_dev(ace->sc_dev, "attach failed\n");
return;
}
aprint_normal_dev(ace->sc_dev, "drive supports %d-sector PIO xfers\n",
ace->sc_multi);
for (blank = 0, p = ace->sc_params.ModelNumber, q = tbuf, i = 0;
i < sizeof(ace->sc_params.ModelNumber); i++) {
c = *p++;
if (c == '\0')
break;
if (c != ' ') {
if (blank) {
*q++ = ' ';
blank = 0;
}
*q++ = c;
} else
blank = 1;
}
*q++ = '\0';
aprint_normal_dev(ace->sc_dev, "card is <%s>\n", tbuf);
int
aceactivate(device_t self, enum devact act)
{
int rv = 0;
switch (act) {
case DVACT_DEACTIVATE:
/*
* Nothing to do; we key off the device's DVF_ACTIVATE.
*/
break;
default:
rv = EOPNOTSUPP;
}
return rv;
}
int
acedetach(device_t self, int flags)
{
struct ace_softc *sc = device_private(self);
int s, bmaj, cmaj, i, mn;
/* locate the major number */
bmaj = bdevsw_lookup_major(&ace_bdevsw);
cmaj = cdevsw_lookup_major(&ace_cdevsw);
/* Nuke the vnodes for any open instances. */
for (i = 0; i < MAXPARTITIONS; i++) {
mn = ACEMINOR(device_unit(self), i);
vdevgone(bmaj, mn, mn, VBLK);
vdevgone(cmaj, mn, mn, VCHR);
}
/* Delete all of our wedges. */
dkwedge_delall(&sc->sc_dk);
s = splbio();
/* Kill off any queued buffers. */
bufq_drain(sc->sc_q);
/* Unhook the entropy source. */
rnd_detach_source(&sc->rnd_source);
#if 0
sc->drvp->drive_flags = 0; /* no drive any more here */
#endif
return 0;
}
/*
* Read/write routine for a buffer. Validates the arguments and schedules the
* transfer. Does not wait for the transfer to complete.
*/
void
acestrategy(struct buf *bp)
{
struct ace_softc *ace;
struct disklabel *lp;
daddr_t blkno;
int s;
/* If device invalidated (e.g. media change, door open), error. */
if ((ace->sc_flags & ACEF_LOADED) == 0) {
bp->b_error = EIO;
goto done;
}
/* If it's a null transfer, return immediately. */
if (bp->b_bcount == 0)
goto done;
/*
* Do bounds checking, adjust transfer. if error, process.
* If end of partition, just return.
*/
if (ACEPART(bp->b_dev) == RAW_PART) {
if (bounds_check_with_mediasize(bp, DEV_BSIZE,
ace->sc_capacity) <= 0)
goto done;
} else {
if (bounds_check_with_label(&ace->sc_dk, bp,
(ace->sc_flags & (ACEF_WLABEL|ACEF_LABELLING)) != 0) <= 0)
goto done;
}
/*
* Now convert the block number to absolute and put it in
* terms of the device's logical block size.
*/
if (lp->d_secsize >= DEV_BSIZE)
blkno = bp->b_blkno / (lp->d_secsize / DEV_BSIZE);
else
blkno = bp->b_blkno * (DEV_BSIZE / lp->d_secsize);
if (ACEPART(bp->b_dev) != RAW_PART)
blkno += lp->d_partitions[ACEPART(bp->b_dev)].p_offset;
bp->b_rawblkno = blkno;
/* Queue transfer on drive, activate drive and controller if idle. */
s = splbio();
bufq_put(ace->sc_q, bp);
acestart(ace);
splx(s);
return;
done:
/* Toss transfer; we're done early. */
bp->b_resid = bp->b_bcount;
biodone(bp);
}
sc->sc_bp = bp;
/*
* If we're retrying, retry in single-sector mode. This will give us
* the sector number of the problem, and will eventually allow the
* transfer to succeed.
*/
if (sc->retries >= ACEIORETRIES_SINGLE)
sc->sc_bio.flags = ATA_SINGLE;
else
sc->sc_bio.flags = 0;
if (bp->b_flags & B_READ)
sc->sc_bio.flags |= ATA_READ;
sc->sc_bio.blkno = bp->b_rawblkno;
sc->sc_bio.blkdone = 0;
sc->sc_bio.nbytes = bp->b_bcount;
sc->sc_bio.nblks = bp->b_bcount >> CF_SECBITS;
sc->sc_bio.databuf = bp->b_data;
/* Instrumentation. */
disk_busy(&sc->sc_dk);
sc->active_xfer = bp;
wakeup(&sc->ch_thread);
}
if (! device_is_active(ace->sc_dev))
return ENODEV;
part = ACEPART(dev);
mutex_enter(&ace->sc_dk.dk_openlock);
/*
* If there are wedges, and this is not RAW_PART, then we
* need to fail.
*/
if (ace->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
error = EBUSY;
goto bad;
}
if (ace->sc_dk.dk_openmask != 0) {
/*
* If any partition is open, but the disk has been invalidated,
* disallow further opens.
*/
if ((ace->sc_flags & ACEF_LOADED) == 0) {
error = EIO;
goto bad;
}
} else {
if ((ace->sc_flags & ACEF_LOADED) == 0) {
ace->sc_flags |= ACEF_LOADED;
/* Load the physical device parameters. */
if (ace->sc_capacity == 0) {
error = sysace_identify(ace);
if (error)
goto bad;
}
/* Load the partition info if not already loaded. */
acegetdisklabel(ace);
}
}
/* Check that the partition exists. */
if (part != RAW_PART &&
(part >= ace->sc_dk.dk_label->d_npartitions ||
ace->sc_dk.dk_label->d_partitions[part].p_fstype == FS_UNUSED)) {
error = ENXIO;
goto bad;
}
/* Insure only one open at a time. */
switch (fmt) {
case S_IFCHR:
ace->sc_dk.dk_copenmask |= (1 << part);
break;
case S_IFBLK:
ace->sc_dk.dk_bopenmask |= (1 << part);
break;
}
ace->sc_dk.dk_openmask =
ace->sc_dk.dk_copenmask | ace->sc_dk.dk_bopenmask;
int
aceclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct ace_softc *ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
int part = ACEPART(dev);
DEBUG_PRINT(("aceclose\n"), DEBUG_FUNCS);
if (ace == NULL)
return ENXIO;
#if DEBUG
if (ACE_DEBUG(DEBUG_WRITES)) {
int i, n = ace->sc_dk.dk_label->d_npartitions;
printf("%s: %d parts\n", device_xname(ace->sc_dev), n);
for (i = 0; i < n; i++) {
printf("\t[%d]: t=%x s=%d o=%d\n", i,
ace->sc_dk.dk_label->d_partitions[i].p_fstype,
ace->sc_dk.dk_label->d_partitions[i].p_size,
ace->sc_dk.dk_label->d_partitions[i].p_offset);
}
}
#endif
if ((flag & FWRITE) == 0) {
return EBADF;
}
if (dks->dks_param != NULL) {
return EINVAL;
}
dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
error = bufq_alloc(&new, dks->dks_name,
BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
if (error) {
return error;
}
s = splbio();
old = ace->sc_q;
bufq_move(new, old);
ace->sc_q = new;
splx(s);
bufq_free(old);
return 0;
}
#ifdef USE_ACE_FOR_RECONFIG
/*
* Ok, how do I get this standardized
* [nothing to do with disks either]
*/
#define DIOC_FPGA_RECONFIGURE _IOW('d',166, struct ioctl_pt)
case DIOC_FPGA_RECONFIGURE:
{
/*
* BUGBUG This is totally wrong, we need to fault in
* all data in advance.
* Otherwise we get back here with the sysace in a bad state
* (its NOT reentrant!)
*/
struct ioctl_pt *pt = (struct ioctl_pt *)addr;
return sysace_send_config(ace,(uint32_t*)pt->data,pt->com);
}
#endif /* USE_ACE_FOR_RECONFIG */
default:
/*
* NB: we get a DIOCGWEDGEINFO, but nobody else handles it
* either
*/
DEBUG_PRINT(("aceioctl: unsup x%lx\n", xfer), DEBUG_FUNCS);
return ENOTTY;
}
}
int
acesize(dev_t dev)
{
struct ace_softc *ace;
int part, omask;
int size;
DEBUG_PRINT(("acesize\n"), DEBUG_FUNCS);
ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
if (ace == NULL)
return -1;
part = ACEPART(dev);
omask = ace->sc_dk.dk_openmask & (1 << part);
/* #define ACE_DUMP_NOT_TRUSTED if you just want to watch */
#define ACE_DUMP_NOT_TRUSTED
static int acedoingadump = 0;
/*
* Dump core after a system crash.
*/
int
acedump(dev_t dev, daddr_t blkno, void *va, size_t size)
{
struct ace_softc *ace; /* disk unit to do the I/O */
struct disklabel *lp; /* disk's disklabel */
int part, err;
int nblks; /* total number of sectors left to write */
/* Check if recursive dump; if so, punt. */
if (acedoingadump)
return EFAULT;
acedoingadump = 1;
ace = device_lookup_private(&ace_cd, ACEUNIT(dev));
if (ace == NULL)
return ENXIO;
part = ACEPART(dev);
/* Convert to disk sectors. Request must be a multiple of size. */
lp = ace->sc_dk.dk_label;
if ((size % lp->d_secsize) != 0)
return EFAULT;
nblks = size / lp->d_secsize;
blkno = blkno / (lp->d_secsize / DEV_BSIZE);
/* Check transfer bounds against partition size. */
if ((blkno < 0) || ((blkno + nblks) > lp->d_partitions[part].p_size))
return EINVAL;
/* Offset block number to start of partition. */
blkno += lp->d_partitions[part].p_offset;