#include <sys/param.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/bus.h>
#include <sys/kernel.h>
#include <sys/kthread.h>
#include <sys/lock.h>
#include <sys/module.h>
#include <sys/mutex.h>
#include <sys/proc.h>
#include <geom/geom_disk.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#include <dev/ofw/ofw_bus.h>
#include <dev/ofw/ofw_bus_subr.h>
#include <dev/ofw/openfirm.h>
#include "opal.h"
struct opalflash_softc {
device_t sc_dev;
struct mtx sc_mtx;
struct disk *sc_disk;
struct proc *sc_p;
struct bio_queue_head sc_bio_queue;
int sc_opal_id;
bool sc_erase;
};
#define OPALFLASH_LOCK(sc) mtx_lock(&(sc)->sc_mtx)
#define OPALFLASH_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx)
#define OPALFLASH_LOCK_INIT(sc) \
mtx_init(&(sc)->sc_mtx, device_get_nameunit((sc)->sc_dev), \
"opalflash", MTX_DEF)
#define FLASH_BLOCKSIZE 512
static int opalflash_probe(device_t);
static int opalflash_attach(device_t);
static device_method_t opalflash_methods[] = {
DEVMETHOD(device_probe, opalflash_probe),
DEVMETHOD(device_attach, opalflash_attach),
DEVMETHOD_END
};
static driver_t opalflash_driver = {
"opalflash",
opalflash_methods,
sizeof(struct opalflash_softc)
};
DRIVER_MODULE(opalflash, opal, opalflash_driver, 0, 0);
static int
opalflash_open(struct disk *dp)
{
return (0);
}
static int
opalflash_close(struct disk *dp)
{
return (0);
}
static int
opalflash_ioctl(struct disk *dp, u_long cmd, void *data, int fflag,
struct thread *td)
{
return (EINVAL);
}
static int
opalflash_getattr(struct bio *bp)
{
struct opalflash_softc *sc;
device_t dev;
if (bp->bio_disk == NULL || bp->bio_disk->d_drv1 == NULL)
return (ENXIO);
sc = bp->bio_disk->d_drv1;
dev = sc->sc_dev;
if (strcmp(bp->bio_attribute, "NAND::device") == 0) {
if (bp->bio_length != sizeof(dev))
return (EFAULT);
bcopy(&dev, bp->bio_data, sizeof(dev));
} else
return (-1);
return (0);
}
static void
opalflash_strategy(struct bio *bp)
{
struct opalflash_softc *sc;
sc = (struct opalflash_softc *)bp->bio_disk->d_drv1;
OPALFLASH_LOCK(sc);
bioq_disksort(&sc->sc_bio_queue, bp);
wakeup(sc);
OPALFLASH_UNLOCK(sc);
}
static int
opalflash_read(struct opalflash_softc *sc, off_t off,
caddr_t data, off_t count)
{
struct opal_msg msg;
int rv, size, token;
if (off % sc->sc_disk->d_sectorsize != 0 ||
count % sc->sc_disk->d_sectorsize != 0)
return (EIO);
token = opal_alloc_async_token();
rv = 0;
while (count > 0) {
size = MIN(count, PAGE_SIZE);
size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
rv = opal_call(OPAL_FLASH_READ, sc->sc_opal_id, off,
vtophys(data), size, token);
if (rv == OPAL_ASYNC_COMPLETION) {
rv = opal_wait_completion(&msg, sizeof(msg), token);
if (rv == OPAL_SUCCESS)
rv = msg.params[1];
}
if (rv != OPAL_SUCCESS)
break;
count -= size;
off += size;
data += size;
}
opal_free_async_token(token);
if (rv == OPAL_SUCCESS)
rv = 0;
else
rv = EIO;
return (rv);
}
static int
opalflash_erase(struct opalflash_softc *sc, off_t off, off_t count)
{
struct opal_msg msg;
int rv, token;
if (off % sc->sc_disk->d_stripesize != 0 ||
count % sc->sc_disk->d_stripesize != 0)
return (EIO);
token = opal_alloc_async_token();
rv = opal_call(OPAL_FLASH_ERASE, sc->sc_opal_id, off, count, token);
if (rv == OPAL_ASYNC_COMPLETION) {
rv = opal_wait_completion(&msg, sizeof(msg), token);
if (rv == OPAL_SUCCESS)
rv = msg.params[1];
}
opal_free_async_token(token);
if (rv == OPAL_SUCCESS)
rv = 0;
else
rv = EIO;
return (rv);
}
static int
opalflash_write(struct opalflash_softc *sc, off_t off,
caddr_t data, off_t count)
{
struct opal_msg msg;
int rv, size, token;
if (off % sc->sc_disk->d_sectorsize != 0 ||
count % sc->sc_disk->d_sectorsize != 0)
return (EIO);
if (sc->sc_erase) {
rv = opalflash_erase(sc, off, count);
if (rv != 0)
return (rv);
}
token = opal_alloc_async_token();
while (count > 0) {
size = MIN(count, PAGE_SIZE);
size = MIN(size, PAGE_SIZE - ((u_long)data & PAGE_MASK));
rv = opal_call(OPAL_FLASH_WRITE, sc->sc_opal_id, off,
vtophys(data), size, token);
if (rv == OPAL_ASYNC_COMPLETION) {
rv = opal_wait_completion(&msg, sizeof(msg), token);
if (rv == OPAL_SUCCESS)
rv = msg.params[1];
}
if (rv != OPAL_SUCCESS)
break;
count -= size;
off += size;
data += size;
}
opal_free_async_token(token);
if (rv == OPAL_SUCCESS)
rv = 0;
else
rv = EIO;
return (rv);
}
static void
opalflash_task(void *arg)
{
struct opalflash_softc *sc;
struct bio *bp;
sc = arg;
for (;;) {
OPALFLASH_LOCK(sc);
do {
bp = bioq_first(&sc->sc_bio_queue);
if (bp == NULL)
msleep(sc, &sc->sc_mtx, PRIBIO, "opalflash", 0);
} while (bp == NULL);
bioq_remove(&sc->sc_bio_queue, bp);
OPALFLASH_UNLOCK(sc);
switch (bp->bio_cmd) {
case BIO_DELETE:
bp->bio_error = opalflash_erase(sc, bp->bio_offset,
bp->bio_bcount);
break;
case BIO_READ:
bp->bio_error = opalflash_read(sc, bp->bio_offset,
bp->bio_data, bp->bio_bcount);
break;
case BIO_WRITE:
bp->bio_error = opalflash_write(sc, bp->bio_offset,
bp->bio_data, bp->bio_bcount);
break;
default:
bp->bio_error = EINVAL;
}
biodone(bp);
}
}
static int
opalflash_probe(device_t dev)
{
if (!ofw_bus_is_compatible(dev, "ibm,opal-flash"))
return (ENXIO);
device_set_desc(dev, "OPAL System Flash");
return (BUS_PROBE_GENERIC);
}
static int
opalflash_attach(device_t dev)
{
struct opalflash_softc *sc;
phandle_t node;
cell_t flash_blocksize, opal_id;
uint32_t regs[2];
sc = device_get_softc(dev);
sc->sc_dev = dev;
node = ofw_bus_get_node(dev);
OF_getencprop(node, "ibm,opal-id", &opal_id, sizeof(opal_id));
sc->sc_opal_id = opal_id;
if (OF_getencprop(node, "ibm,flash-block-size",
&flash_blocksize, sizeof(flash_blocksize)) < 0) {
device_printf(dev, "Cannot determine flash block size.\n");
return (ENXIO);
}
if (!OF_hasprop(node, "no-erase"))
sc->sc_erase = true;
OPALFLASH_LOCK_INIT(sc);
if (OF_getencprop(node, "reg", regs, sizeof(regs)) < 0) {
device_printf(dev, "Unable to get flash size.\n");
return (ENXIO);
}
sc->sc_disk = disk_alloc();
sc->sc_disk->d_name = "opalflash";
sc->sc_disk->d_open = opalflash_open;
sc->sc_disk->d_close = opalflash_close;
sc->sc_disk->d_strategy = opalflash_strategy;
sc->sc_disk->d_ioctl = opalflash_ioctl;
sc->sc_disk->d_getattr = opalflash_getattr;
sc->sc_disk->d_drv1 = sc;
sc->sc_disk->d_maxsize = DFLTPHYS;
sc->sc_disk->d_mediasize = regs[1];
sc->sc_disk->d_unit = device_get_unit(sc->sc_dev);
sc->sc_disk->d_sectorsize = FLASH_BLOCKSIZE;
sc->sc_disk->d_stripesize = flash_blocksize;
sc->sc_disk->d_dump = NULL;
disk_create(sc->sc_disk, DISK_VERSION);
bioq_init(&sc->sc_bio_queue);
kproc_create(&opalflash_task, sc, &sc->sc_p, 0, 0, "task: OPAL Flash");
return (0);
}