/*-
* 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.
*/
static const struct flash_type sector_maps[] = {
{
{{32,128},{0,0},},
MANUF_INTEL, DEVICE_320, 32, /* a J3 part */
"StrataFlash 28F320"
},
{
{{64,128},{0,0},},
MANUF_INTEL, DEVICE_640, 64, /* a J3 part */
"StrataFlash 28F640"
},
{
{{128,128},{0,0},},
MANUF_INTEL, DEVICE_128, 128, /* a J3 part */
"StrataFlash 28F128"
},
{
{{255,128},{4,32},{0,0}},
MANUF_INTEL, DEVICE_256, 259, /* a P30 part */
"StrataFlash 28F256"
}
};
#define nMAPS ((sizeof sector_maps) / (sizeof sector_maps[0]))
/* Instead of dragging in atavar.h.. */
struct eflash_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;
u_int32_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*/
/* General disk infos */
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 */
struct eflash_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 sc_flags;
#define EFLASHF_WLABEL 0x004 /* label is writable */
#define EFLASHF_LABELLING 0x008 /* writing label */
#define EFLASHF_LOADED 0x010 /* parameters loaded */
#define EFLASHF_WAIT 0x020 /* waiting for resources */
#define EFLASHF_KLABEL 0x080 /* retain label after 'full' close */
/* Plan.
* - mips_map_physmem() (with uncached) first page
* - keep it around since we need status ops
* - find what type it is.
* - then mips_map_physmem() each sector as needed.
*/
sc->sc_size = ctrl & FLASHST_SIZE;
sc->sc_capacity = sc->sc_size / DEV_BSIZE;
sc->sc_base = base;
/* The chip is 16bit, so if we get 32bit there are two */
sc->sc_chips = (ctrl & FLASHST_BUS_32) ? 2 : 1;
/* Map the first page to see what chip we got */
sc->sc_page0 = (volatile uint8_t *) mips_map_physmem(base, PAGE_SIZE);
if (flash_identify(sc)) {
printf(" base %x: %dMB flash memory (%d x %s)\n", base, sc->sc_size >> 20,
sc->sc_chips, sc->sc_type.ft_name);
} else {
/* BUGBUG If we dont identify it stop the driver! */
printf(": unknown manufacturer id %x, device id %x\n",
sc->sc_type.ft_manuf_code, sc->sc_type.ft_device_code);
}
/* Implementation functions
*/
/* Returns the size in KBytes of a given sector,
* or -1 for bad arguments.
*/
static int KBinSector(struct flash_type * SecMap, unsigned int SecNo)
{
int i;
for (i = 0; i < nDELTAS; i++) {
if (SecNo < SecMap->ft_deltas[i].nSectors)
return SecMap->ft_deltas[i].nKB;
SecNo -= SecMap->ft_deltas[i].nSectors;
}
/* Whats the starting offset of sector N
*/
static uint32_t SectorStart(struct flash_type * SecMap, int SecNo)
{
int i;
uint32_t Offset = 0;
for (i = 0; i < nDELTAS; i++) {
if ((unsigned int)SecNo < SecMap->ft_deltas[i].nSectors)
return 1024 * (Offset + (SecMap->ft_deltas[i].nKB * SecNo));
SecNo -= SecMap->ft_deltas[i].nSectors;
Offset += SecMap->ft_deltas[i].nSectors * SecMap->ft_deltas[i].nKB;
}
return ~0;
}
/* What sector number corresponds to a given offset
*/
static unsigned int SectorNumber(struct flash_type * SecMap, uint32_t Offset)
{
unsigned int i;
unsigned int SecNo = 0;
Offset /= 1024;
for (i = 0; i < nDELTAS; i++) {
if (Offset < (unsigned int)
((SecMap->ft_deltas[i].nSectors * SecMap->ft_deltas[i].nKB)))
return SecNo + (Offset / SecMap->ft_deltas[i].nKB);
SecNo += SecMap->ft_deltas[i].nSectors;
Offset -= SecMap->ft_deltas[i].nSectors * SecMap->ft_deltas[i].nKB;
}
static void single_read_uint32 (struct eflash_softc *sc,volatile void *Offset,uint32_t *Value)
{
/* back-to-back reads must be ok */
volatile uint32_t * Where = Offset;
*Value = *Where;
}
/*
* Hardware access proper, paired-chips
* NB: This set of ops assumes two chips in parallel on a 32bit bus,
* each operation is repeated in parallel to both chips
*/
static void twin_write_uint8 (struct eflash_softc *sc,volatile void *Offset,uint8_t Value)
{
volatile uint32_t * Where = Offset;
uint32_t v = Value | ((uint32_t)Value << 16);
v = le32toh(v);
*Where = v;
}
static void twin_read_uint8 (struct eflash_softc *sc,volatile void *Offset,uint8_t *Value)
{
volatile uint32_t * Where = Offset;
uint32_t v;
v = *Where;
v = le32toh(v);
*Value = (uint8_t) v;
}
/* This one should *not* be used, error-prone */
static void twin_write_uint16 (struct eflash_softc *sc,volatile void *Offset,uint16_t Value)
{
volatile uint16_t * Where = Offset;
*Where = Value;
}
static void PrintStatus(uint8_t Status)
{
/* BUGBUG there's a %b format I think? */
string BitNames[8] = {
"reserved", "BLOCK_LOCKED",
"PROGRAM_SUSPENDED", "LOW_VOLTAGE",
"LOCK_BIT_ERROR", "ERASE_ERROR",
"ERASE_SUSPENDED", "READY"
};
int i;
int OneSet = FALSE;
printf("[status %x =",Status);
for (i = 0; i < 8; i++) {
if (Status & (1<<i)) {
printf("%c%s",
(OneSet) ? '|' : ' ',
BitNames[i]);
OneSet = TRUE;
}
}
printf("]\n");
}
#else
#define PrintStatus(x)
#endif
/*
* The device can lock up under certain conditions.
* There is no software workaround [must toggle RP# to GND]
* Check if it seems that we are in that state.
*/
static int IsIrresponsive(struct eflash_softc *sc)
{
uint8_t Status = ReadStatusRegister(sc);
if (Status & ST_READY)
return FALSE;
if ((Status & ST_MASK) ==
(ST_LOCK_BIT_ERROR|ST_ERASE_SUSPENDED|ST_ERASE_ERROR)) {
/* yes, looks that way */
return TRUE;
}
/* Something is indeed amiss, but we dont really know for sure */
PrintStatus(ReadStatusRegister(sc));
ClearStatusRegister(sc);
PrintStatus(ReadStatusRegister(sc));
if ((Status & ST_MASK) ==
(ST_LOCK_BIT_ERROR|ST_ERASE_SUSPENDED|ST_ERASE_ERROR)) {
/* yes, looks that way */
return TRUE;
}
return FALSE;
}
/* Write one 16bit word
*/
static int
single_program_word(struct eflash_softc *sc, volatile void *Offset, uint16_t *Values,
int Verify, int *nWritten)
{
uint8_t Status;
uint16_t i, Data16, Value;
*nWritten = 0;
Value = Values[0];
if (Verify) {
sc->sc_ops->read_uint16(sc,Offset,&Data16);
#ifdef Verbose
if (Verbose) {
printf("Location %p was x%x\n",
Offset, Data16);
}
#endif
if (Data16 != 0xffff)
printf("Offset %p not ERASED, wont take.\n",Offset);
}
/* Wait until the operation is completed
* Specs say it takes between 210 and 630 us
* Errata says 360 TYP and Max=TBD (sic)
*/
DELAY(800);
for (i = 0; i < 10; i++) {
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
DELAY(100);
}
ProductIdExit(sc);
if (Verify) {
sc->sc_ops->read_uint16(sc,Offset,&Data16);
#ifdef Verbose
if (Verbose) {
printf("Location %p is now x%x\n",
Offset, Data16);
}
#endif
if ((Data16 != Value)) {
PrintStatus(Status);
printf(". That didnt work, try again.. [%x != %x]\n",
Data16, Value);
ClearStatusRegister(sc);
return FALSE;
}
}
*nWritten = 2;
return TRUE;
}
/* Write one buffer, 16bit words at a time
*/
static int
single_program_buffer(struct eflash_softc *sc, volatile void *Offset, uint16_t *Values,
int Verify, int *nWritten)
{
uint8_t Status;
uint16_t i, Data16, Value = 0;
volatile uint8_t *Where = Offset;
if (Verify) {
for (i = 0; i < sc->sc_buffersize; i+= 2) {
sc->sc_ops->read_uint16(sc,Where+i,&Data16);
#ifdef Verbose
if (Verbose) {
printf("Location %p was x%x\n",
Where+i, Data16);
}
#endif
if (Data16 != 0xffff)
printf("Offset %p not ERASED, wont take.\n",Where+i);
}
}
/* Specs say to retry if necessary */
for (i = 0; i < 5; i++) {
sc->sc_ops->write_uint8(sc,Offset,CMD_WRITE_BUFFER);
DELAY(10);
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
}
if (0 == (Status & ST_READY)) {
printf("FAILED program_buffer at Location %p, Status= x%x\n",
Offset, Status);
return FALSE;
}
/* Say how many words we'll be sending */
sc->sc_ops->write_uint8(sc,Offset,(uint8_t)(sc->sc_buffersize/2));
/* Send the data */
for (i = 0; i < sc->sc_buffersize; i+= 2) {
Value = Values[i/2];
sc->sc_ops->write_uint16(sc,Where+i,Value);
DELAY(10);/*jic*/
}
/* Wait until the operation is completed
* Specs say it takes between 800 and 2400 us
* Errata says 1600 TYP and Max=TBD (sic), but fixed in stepping A3 and above.
*/
DELAY(800);
for (i = 0; i < 20; i++) {
sc->sc_ops->write_uint8(sc,Offset,CMD_READ_STATUS);
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
DELAY(200);
}
ProductIdExit(sc);
/* Verify? */
if (Verify) {
for (i = 0; i < sc->sc_buffersize; i+= 2) {
sc->sc_ops->read_uint16(sc,Where+i,&Data16);
#ifdef Verbose
if (Verbose) {
printf("Location %p is now x%x\n",
Where+i, Data16);
}
#endif
Value = Values[i/2];
/* Write one 32bit word
*/
static int
twin_program_word(struct eflash_softc *sc, volatile void *Offset, uint16_t *Values,
int Verify, int *nWritten)
{
uint8_t Status;
uint32_t i, Data32, Value;
uint16_t v0, v1;
*nWritten = 0;
v0 = Values[0];
v0 = le16toh(v0);
v1 = Values[1];
v1 = le16toh(v1);
Value = v0 | ((uint32_t)v1 << 16);
if (Verify) {
sc->sc_ops->read_uint32(sc,Offset,&Data32);
#ifdef Verbose
if (Verbose) {
printf("Location %p was x%x\n",
Offset, Data32);
}
#endif
if (Data32 != 0xffffffff)
printf("Offset %p not ERASED, wont take.\n",Offset);
}
/* Wait until the operation is completed
* Specs say it takes between 210 and 630 us
* Errata says 360 TYP and Max=TBD (sic)
*/
DELAY(400);
for (i = 0; i < 10; i++) {
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
DELAY(100);
}
ProductIdExit(sc);
if (Verify) {
sc->sc_ops->read_uint32(sc,Offset,&Data32);
#ifdef Verbose
if (Verbose) {
printf("Location %p is now x%x\n",
Offset, Data32);
}
#endif
if ((Data32 != Value)) {
PrintStatus(Status);
printf(". That didnt work, try again.. [%x != %x]\n",
Data32, Value);
ClearStatusRegister(sc);
return FALSE;
}
}
*nWritten = 4;
return TRUE;
}
/* Write one buffer, 32bit words at a time
*/
static int
twin_program_buffer(struct eflash_softc *sc, volatile void *Offset, uint16_t *Values,
int Verify, int *nWritten)
{
uint8_t Status;
uint32_t i, Data32, Value;
uint16_t v0 = 0, v1;
volatile uint8_t *Where = Offset;
if (Verify) {
for (i = 0; i < sc->sc_buffersize; i+= 4) {
sc->sc_ops->read_uint32(sc,Where+i,&Data32);
#ifdef Verbose
if (Verbose) {
printf("Location %p was x%x\n",
Where+i, Data32);
}
#endif
if (Data32 != 0xffffffff)
printf("Offset %p not ERASED, wont take.\n",Where+i);
}
}
/* Specs say to retry if necessary */
for (i = 0; i < 5; i++) {
sc->sc_ops->write_uint8(sc,Offset,CMD_WRITE_BUFFER);
DELAY(10);
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
}
if (0 == (Status & ST_READY)) {
printf("FAILED program_buffer at Location %p, Status= x%x\n",
Offset, Status);
return FALSE;
}
/* Say how many words we'll be sending */
sc->sc_ops->write_uint8(sc,Offset,(uint8_t)(sc->sc_buffersize/4)); /* to each twin! */
/* Send the data */
for (i = 0; i < sc->sc_buffersize; i+= 4) {
v0 = Values[i/2];
v0 = le16toh(v0);
v1 = Values[1+(i/2)];
v1 = le16toh(v1);
Value = v0 | ((uint32_t)v1 << 16);
sc->sc_ops->write_uint32(sc,Where+i,Value);
DELAY(10);/*jic*/
}
/* Wait until the operation is completed
* Specs say it takes between 800 and 2400 us
* Errata says 1600 TYP and Max=TBD (sic), but fixed in stepping A3 and above.
*/
DELAY(800);
for (i = 0; i < 20; i++) {
sc->sc_ops->write_uint8(sc,Offset,CMD_READ_STATUS);
sc->sc_ops->read_uint8(sc,Offset,&Status);
if ((Status & ST_READY)) break;
DELAY(200);
}
ProductIdExit(sc);
/* Verify */
if (Verify) {
for (i = 0; i < sc->sc_buffersize; i+= 4) {
sc->sc_ops->read_uint32(sc,Where+i,&Data32);
#ifdef Verbose
if (Verbose) {
printf("Location %p is now x%x\n",
Where+i, Data32);
}
#endif
v0 = Values[i/2];
v0 = le16toh(v0);
v1 = Values[1+(i/2)];
v1 = le16toh(v1);
Value = v0 | ((uint32_t)v1 << 16);
/* Is there a lock on a given sector
*/
static int IsSectorLocked(struct eflash_softc *sc, uint8_t *secptr)
{
uint8_t Data, Data1;
ProductIdEnter(sc);
/* Lockout info is at address 2 of the given sector, meaning A0=0 A1=1.
*/
sc->sc_ops->read_uint8(sc,secptr+(0x0002*2*sc->sc_chips),&Data);
sc->sc_ops->read_uint8(sc,secptr+(0x0003*2*sc->sc_chips),&Data1);
ProductIdExit(sc);
return (Data & 1);
}
/* Remove the write-lock to a sector
*/
static void SectorUnLock(struct eflash_softc *sc, uint8_t *secptr)
{
uint8_t Status;
int i;
/* Wait until the erase is actually completed
* Specs say it will take between 1 and 5 seconds.
* Errata says it takes 2 sec min and 25 sec max.
* Double that before giving up.
*/
for (i = 0; i < 20; i++) {
/* Sleep for at least 2 seconds
*/
tsleep(sc,PWAIT,"erase", hz * 2);
sc->sc_ops->read_uint8(sc,secptr,&Status);
if ((Status & ST_READY)) break;
PrintStatus(Status);
}
PrintStatus(Status);
DBGME(DEBUG_ERRORS,printf("%s: Erase of sector %d NOT completed (status=%x).\n",
device_xname(sc->sc_dev),
sc->sc_sector_offset, Status));
ClearStatusRegister(sc);
return EIO;
}
/* Write (a portion of) a sector
*/
static size_t eflash_write_sector(struct eflash_softc *sc, char *Buffer, size_t n,
uint8_t *Offset, int Verify)
{
size_t i;
/* Make sure the device is not screwed up
*/
if (IsIrresponsive(sc)) {
printf("FLASH is locked-up (or mapped cacheable?), wont work. ");
}
for (i = 0; i < n;) {
int nTries;
int nWritten = 0;/*we expect 2 or 4 */
if (sc->sc_buffersize && ((n-i) >= sc->sc_buffersize)) {
for (nTries = 0; nTries < 5; nTries++)
if (sc->sc_ops->program_buffer(sc,Offset,(uint16_t*)(Buffer+i),Verify,&nWritten))
break;
} else {
for (nTries = 0; nTries < 5; nTries++)
if (sc->sc_ops->program_word(sc,Offset,(uint16_t*)(Buffer+i),Verify,&nWritten))
break;
}
Offset += nWritten;
i += nWritten;
if (nWritten == 0)
break;
}
return i;
}
/* Identify type and the sector map of the FLASH.
* Argument is the base address of the device and the count of chips on the bus (1/2)
* Returns FALSE if failed
*/
static const struct flash_ops single_ops = {
single_write_uint8,
single_read_uint8,
single_write_uint16,
single_read_uint16,
single_write_uint32,
single_read_uint32,
single_program_word,
single_program_buffer
};
/* Update state. We have to assume the sector was not erased. Sigh. */
sc->sc_sector_offset = sec;
sc->sc_sector_size = secsize;
sc->sc_erased = FALSE;
}
/* Adjust size if necessary
*/
Size = start + *pSize; /* last sector */
if (Size > sc->sc_capacity) {
/* At most this many sectors
*/
Size = sc->sc_capacity - start;
*pSize = (size_t)Size;
}
if (*pSize > (secsize >> DEV_BSHIFT)) {
*pSize = secsize >> DEV_BSHIFT;
}
Out:
if (pSizeWritten)
*pSizeWritten = SizeWritten;
return error;
}
/* 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 EFLASHIORETRIES_SINGLE 4 /* number of retries before single-sector */
#define EFLASHIORETRIES 5 /* number of retries before giving up */
#define RECOVERYTIME hz/2 /* time to wait before retrying a cmd */
#define EFLASHUNIT(dev) DISKUNIT(dev)
#define EFLASHPART(dev) DISKPART(dev)
#define EFLASHMINOR(unit, part) DISKMINOR(unit, part)
#define MAKEEFLASHDEV(maj, unit, part) MAKEDISKDEV(maj, unit, part)
/* setup all required fields so that if the attach fails we are ok */
sc->sc_dk.dk_driver = &eflashdkdriver;
sc->sc_dk.dk_name = device_xname(sc->sc_dev);
/* locate the major number */
bmaj = bdevsw_lookup_major(&eflash_bdevsw);
cmaj = cdevsw_lookup_major(&eflash_cdevsw);
/* Nuke the vnodes for any open instances. */
for (i = 0; i < MAXPARTITIONS; i++) {
mn = EFLASHMINOR(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);
/*sc->atabus->ata_killpending(sc->drvp);*/
splx(s);
bufq_free(sc->sc_q);
/* Detach disk. */
disk_detach(&sc->sc_dk);
/* Unhook the entropy source. */
rnd_detach_source(&sc->rnd_source);
/*sc->drvp->drive_flags = 0; -- no drive any more here */
return (0);
}
extern int dkwedge_autodiscover;
/* Aux temp thread to avoid deadlock when doing the partitio.. ahem wedges thing.
*/
static void
eflash_wedges(void *arg)
{
struct eflash_softc *sc = (struct eflash_softc*)arg;
DBGME(DEBUG_STATUS,printf("%s: wedges started for %p\n", sc->sc_dk.dk_name, sc));
/* Discover wedges on this disk. */
dkwedge_autodiscover = 1;
dkwedge_discover(&sc->sc_dk);
config_pending_decr(sc->sc_dev);
DBGME(DEBUG_STATUS,printf("%s: wedges thread done for %p\n", device_xname(sc->sc_dev), sc));
kthread_exit(0);
}
DBGME(DEBUG_STATUS,printf("%s: thread started for %p\n", device_xname(sc->sc_dev), sc));
s = splbio();
eflashattach(sc);
splx(s);
/* Allocate a VM window large enough to map the largest sector
* BUGBUG We could risk it and allocate/free on open/close?
*/
addr = uvm_km_alloc(kernel_map, sc->sc_max_secsize, 0, UVM_KMF_VAONLY);
if (addr == 0)
panic("eflash_thread: kernel map full (%lx)", (long unsigned)sc->sc_max_secsize);
sc->sc_sector = (/*volatile*/ uint8_t *) addr;
sc->sc_sector_size = 0;
sc->sc_sector_offset = NOSECTOR;
DBGME(DEBUG_STATUS,printf("%s: thread service terminated for %p\n", device_xname(sc->sc_dev), sc));
kthread_exit(0);
}
/*
* Read/write routine for a buffer. Validates the arguments and schedules the
* transfer. Does not wait for the transfer to complete.
*/
void
eflashstrategy(struct buf *bp)
{
struct eflash_softc *sc = device_lookup_private(&eflash_cd, EFLASHUNIT(bp->b_dev));
struct disklabel *lp = sc->sc_dk.dk_label;
daddr_t blkno;
int s;
/* If device invalidated (e.g. media change, door open), error. */
if ((sc->sc_flags & EFLASHF_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 (EFLASHPART(bp->b_dev) == RAW_PART) {
if (bounds_check_with_mediasize(bp, DEV_BSIZE,
sc->sc_capacity) <= 0)
goto done;
} else {
if (bounds_check_with_label(&sc->sc_dk, bp,
(sc->sc_flags & (EFLASHF_WLABEL|EFLASHF_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 (EFLASHPART(bp->b_dev) != RAW_PART)
blkno += lp->d_partitions[EFLASHPART(bp->b_dev)].p_offset;
bp->b_rawblkno = blkno;
/* Queue transfer on drive, activate drive and controller if idle. */
s = splbio();
bufq_put(sc->sc_q, bp);
eflashstart(sc);
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 >= EFLASHIORETRIES_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 >> DEV_BSHIFT;
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(sc->sc_dev))
return (ENODEV);
part = EFLASHPART(dev);
mutex_enter(&sc->sc_dk.dk_openlock);
/*
* If there are wedges, and this is not RAW_PART, then we
* need to fail.
*/
if (sc->sc_dk.dk_nwedges != 0 && part != RAW_PART) {
error = EBUSY;
goto bad;
}
if (sc->sc_dk.dk_openmask != 0) {
/*
* If any partition is open, but the disk has been invalidated,
* disallow further opens.
*/
if ((sc->sc_flags & EFLASHF_LOADED) == 0) {
error = EIO;
goto bad;
}
} else {
if ((sc->sc_flags & EFLASHF_LOADED) == 0) {
sc->sc_flags |= EFLASHF_LOADED;
/* Load the partition info if not already loaded. */
eflashgetdisklabel(sc);
}
}
/* Check that the partition exists. */
if (part != RAW_PART &&
(part >= sc->sc_dk.dk_label->d_npartitions ||
sc->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:
sc->sc_dk.dk_copenmask |= (1 << part);
break;
case S_IFBLK:
sc->sc_dk.dk_bopenmask |= (1 << part);
break;
}
sc->sc_dk.dk_openmask =
sc->sc_dk.dk_copenmask | sc->sc_dk.dk_bopenmask;
int
eflashclose(dev_t dev, int flag, int fmt, struct lwp *l)
{
struct eflash_softc *sc = device_lookup_private(&eflash_cd, EFLASHUNIT(dev));
int part = EFLASHPART(dev);
/* BUGBUG: maj==0?? why is this not EFLASHLABELDEV(??sc->sc_dev) */
errstring = readdisklabel(MAKEEFLASHDEV(0, device_unit(sc->sc_dev),
RAW_PART), eflashstrategy, lp,
sc->sc_dk.dk_cpulabel);
if (errstring) {
printf("%s: %s\n", device_xname(sc->sc_dev), errstring);
return;
}
#if DEBUG
if (EFLASH_DEBUG(DEBUG_WRITES)) {
int i, n = sc->sc_dk.dk_label->d_npartitions;
printf("%s: %d parts\n", device_xname(sc->sc_dev), n);
for (i = 0; i < n; i++) {
printf("\t[%d]: t=%x s=%d o=%d\n", i,
sc->sc_dk.dk_label->d_partitions[i].p_fstype,
sc->sc_dk.dk_label->d_partitions[i].p_size,
sc->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 = sc->sc_q;
bufq_move(new, old);
sc->sc_q = new;
splx(s);
bufq_free(old);
return 0;
}
default:
/* NB: we get a DIOCGWEDGEINFO, but nobody else handles it either */
DEBUG_PRINT(("eflashioctl: unsup x%lx\n", xfer), DEBUG_FUNCS);
return ENOTTY;
}
}
int
eflashsize(dev_t dev)
{
struct eflash_softc *sc;
int part, omask;
int size;
DEBUG_PRINT(("eflashsize\n"), DEBUG_FUNCS);
sc = device_lookup_private(&eflash_cd, EFLASHUNIT(dev));
if (sc == NULL)
return (-1);
part = EFLASHPART(dev);
omask = sc->sc_dk.dk_openmask & (1 << part);