#include <sys/param.h>
#include <sys/time.h>
#include "libsa.h"
#include <sys/reboot.h>
#include <sys/disklabel.h>
#include <machine/trap.h>
#include <machine/pdc.h>
#include <machine/iomod.h>
#include <machine/cpufunc.h>
#include "dev_hppa.h"
pdcio_t pdc;
int pdcbuf[64] PDC_ALIGNMENT;
struct stable_storage sstor;
int sstorsiz;
void
pdc_init()
{
int err;
pdc = (pdcio_t)PAGE0->mem_pdc;
err = (*pdc)(PDC_STABLE, PDC_STABLE_SIZE, pdcbuf, 0, 0);
if (err >= 0) {
sstorsiz = min(pdcbuf[0],sizeof(sstor));
err = (*pdc)(PDC_STABLE, PDC_STABLE_READ, 0, &sstor, sstorsiz);
}
#ifdef DEBUG
if (debug && err)
printf("Stable storage PDC_STABLE Read Ret'd %d\n", err);
#endif
(*pdc)(PDC_CHASSIS, PDC_CHASSIS_DISP,
PDC_OSTAT(PDC_OSTAT_BOOT) | 0xCEC0);
}
int
iodcstrategy(devdata, rw, blk, size, buf, rsize)
void *devdata;
int rw;
daddr_t blk;
size_t size;
void *buf;
size_t *rsize;
{
struct hppa_dev *dp = devdata;
struct pz_device *pzdev = dp->pz_dev;
int offset, xfer, ret;
#ifdef PDCDEBUG
if (debug)
printf("iodcstrategy(%p, %s, %u, %u, %p, %p)\n", devdata,
rw==F_READ? "READ" : "WRITE", blk, size, buf, rsize);
if (debug > 1)
PZDEV_PRINT(pzdev);
#endif
blk += dp->fsoff;
blk *= DEV_BSIZE;
if ((pzdev->pz_class & PCL_CLASS_MASK) == PCL_SEQU) {
if (blk < dp->last_blk) {
#ifdef PDCDEBUG
if (debug)
printf("iodc: rewind ");
#endif
if ((ret = ((iodcio_t)pzdev->pz_iodc_io)(pzdev->pz_hpa,
IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
pdcbuf, 0, dp->buf, 0, 0)) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_IO: %d\n", ret);
#endif
return (EIO);
} else {
dp->last_blk = 0;
dp->last_read = 0;
}
}
#ifdef PDCDEBUG
if (debug)
printf("seek %d ", dp->last_blk);
#endif
for (; (dp->last_blk + dp->last_read) <= blk;
dp->last_read = ret) {
twiddle();
dp->last_blk += dp->last_read;
if ((ret = ((iodcio_t)pzdev->pz_iodc_io)(pzdev->pz_hpa,
IODC_IO_READ, pzdev->pz_spa, pzdev->pz_layers,
pdcbuf, dp->last_blk, dp->buf, IODC_IOSIZ,
IODC_IOSIZ)) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_IO: %d\n", ret);
#endif
return (EIO);
}
if ((ret = pdcbuf[0]) == 0)
break;
#ifdef PDCDEBUG
if (debug)
printf("-");
#endif
}
#ifdef PDCDEBUG
if (debug)
printf("> %d[%d]\n", dp->last_blk, dp->last_read);
#endif
}
xfer = 0;
if (rw == F_READ &&
dp->last_blk <= blk && (dp->last_blk + dp->last_read) > blk) {
twiddle();
offset = blk - dp->last_blk;
xfer = min(dp->last_read - offset, size);
size -= xfer;
blk += xfer;
#ifdef PDCDEBUG
if (debug)
printf("off=%d,xfer=%d,size=%d,blk=%d\n",
offset, xfer, size, blk);
#endif
bcopy(dp->buf + offset, buf, xfer);
buf += xfer;
}
for (; size; size -= ret, buf += ret, blk += ret, xfer += ret) {
offset = blk & IOPGOFSET;
if (rw != F_READ) {
bcopy(buf, dp->buf, size);
dp->last_blk = 0;
dp->last_read = 0;
}
if ((ret = ((iodcio_t)pzdev->pz_iodc_io)(pzdev->pz_hpa,
(rw == F_READ? IODC_IO_READ: IODC_IO_WRITE),
pzdev->pz_spa, pzdev->pz_layers, pdcbuf,
(u_int)blk - offset, dp->buf, IODC_IOSIZ, IODC_IOSIZ)) < 0) {
#ifdef DEBUG
if (debug)
printf("iodc_read(%d,%d): %d\n",
blk - offset, IODC_IOSIZ, ret);
#endif
if (xfer)
break;
else
return (EIO);
}
if ((ret = pdcbuf[0]) <= 0)
break;
if (rw == F_READ) {
dp->last_blk = blk - offset;
dp->last_read = ret;
if ((ret -= offset) > size)
ret = size;
bcopy(dp->buf + offset, buf, ret);
}
#ifdef PDCDEBUG
if (debug)
printf("read %d(%d,%d)@%x ", ret,
dp->last_blk, dp->last_read, (u_int)buf);
#endif
}
#ifdef PDCDEBUG
if (debug)
printf("\n");
#endif
if (rsize)
*rsize = xfer;
return (0);
}
struct pz_device *
pdc_findev(unit, class)
int unit, class;
{
static struct pz_device pz;
int layers[sizeof(pz.pz_layers)/sizeof(pz.pz_layers[0])];
struct iomod *io;
iodcio_t iodc;
int err = 0;
#ifdef PDCDEBUG
if (debug)
printf("pdc_finddev(%d, %x)\n", unit, class);
#endif
iodc = (iodcio_t)(PAGE0->mem_free + IODC_MAXSIZE);
io = (struct iomod *)PAGE0->mem_boot.pz_hpa;
if (PAGE0->mem_boot.pz_class == class &&
(unit == -1 || PAGE0->mem_boot.pz_layers[0] == unit)) {
bcopy (&PAGE0->mem_boot.pz_dp, &pz.pz_dp, sizeof(pz.pz_dp));
bcopy (pz.pz_layers, layers, sizeof(layers));
if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_READ: %d\n", err);
#endif
return NULL;
}
} else {
struct pdc_memmap memmap;
struct iodc_data mptr;
int i, stp;
for (i = 0; i < 0xf; i++) {
pz.pz_bc[0] = pz.pz_bc[1] =
pz.pz_bc[2] = pz.pz_bc[3] = -1;
pz.pz_bc[4] = 2;
pz.pz_bc[5] = 0;
pz.pz_mod = i;
if ((pdc)(PDC_MEMMAP, PDC_MEMMAP_HPA, &memmap,
&pz.pz_dp) < 0)
continue;
#ifdef PDCDEBUG
if (debug)
printf("memap: %d.%d.%d, hpa=%x, mpgs=%x\n",
pz.pz_bc[4], pz.pz_bc[5], pz.pz_mod,
memmap.hpa, memmap.morepages);
#endif
io = (struct iomod *) memmap.hpa;
if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, &pdcbuf, io,
IODC_DATA, &mptr, sizeof(mptr))) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_DATA: %d\n", err);
#endif
continue;
}
if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
IODC_INIT, iodc, IODC_MAXSIZE)) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_READ: %d\n", err);
#endif
continue;
}
stp = IODC_INIT_FIRST;
do {
if ((err = (iodc)((u_int)io, stp, io->io_spa,
layers, pdcbuf, 0, 0, 0, 0)) < 0) {
#ifdef DEBUG
if (debug && err != PDC_ERR_EOD)
printf("IODC_INIT_%s: %d\n",
stp==IODC_INIT_FIRST?
"FIRST":"NEXT", err);
#endif
break;
}
#ifdef PDCDEBUG
if (debug)
printf("[%x,%x,%x,%x,%x,%x], "
"[%x,%x,%x,%x,%x,%x]\n",
pdcbuf[0], pdcbuf[1], pdcbuf[2],
pdcbuf[3], pdcbuf[4], pdcbuf[5],
layers[0], layers[1], layers[2],
layers[3], layers[4], layers[5]);
#endif
stp = IODC_INIT_NEXT;
} while (pdcbuf[1] != class &&
unit != -1 && unit != layers[0]);
if (err >= 0)
break;
}
}
if (err >= 0) {
if (0 && (err = (iodc)((u_int)io, IODC_INIT_DEV, io->io_spa,
layers, pdcbuf, 0, 0, 0, 0)) < 0) {
#ifdef DEBUG
if (debug)
printf("INIT_DEV: %d\n", err);
#endif
return NULL;
}
if ((err = (pdc)(PDC_IODC, PDC_IODC_READ, pdcbuf, io,
IODC_IO, iodc, IODC_MAXSIZE)) < 0) {
#ifdef DEBUG
if (debug)
printf("IODC_READ: %d\n", err);
#endif
return NULL;
}
pz.pz_flags = 0;
bcopy(layers, pz.pz_layers, sizeof(pz.pz_layers));
pz.pz_hpa = (u_int)io;
pz.pz_iodc_io = (u_int)iodc;
pz.pz_class = class;
return &pz;
}
return NULL;
}
static __inline void
fall(int c_base, int c_count, int c_loop, int c_stride, int data)
{
int loop;
for (; c_count--; c_base += c_stride)
for (loop = c_loop; loop--; )
if (data)
fdce(0, c_base);
else
fice(0, c_base);
}
struct pdc_cache pdc_cacheinfo PDC_ALIGNMENT;
void
fcacheall()
{
int err;
if ((err = (*pdc)(PDC_CACHE, PDC_CACHE_DFLT, &pdc_cacheinfo)) < 0) {
#ifdef DEBUG
if (debug)
printf("fcacheall: PDC_CACHE failed (%d).\n", err);
#endif
return;
}
#if PDCDEBUG
if (debug)
printf("pdc_cache:\nic={%u,%x,%x,%u,%u,%u}\n"
"dc={%u,%x,%x,%u,%u,%u}\n",
pdc_cacheinfo.ic_size, *(u_int *)&pdc_cacheinfo.ic_conf,
pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_stride,
pdc_cacheinfo.ic_count, pdc_cacheinfo.ic_loop,
pdc_cacheinfo.dc_size, *(u_int *)&pdc_cacheinfo.ic_conf,
pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_stride,
pdc_cacheinfo.dc_count, pdc_cacheinfo.dc_loop);
#endif
fall(pdc_cacheinfo.ic_base, pdc_cacheinfo.ic_count,
pdc_cacheinfo.ic_loop, pdc_cacheinfo.ic_stride, 0);
sync_caches();
fall(pdc_cacheinfo.dc_base, pdc_cacheinfo.dc_count,
pdc_cacheinfo.dc_loop, pdc_cacheinfo.dc_stride, 1);
sync_caches();
}