/******************************************************************************
 * Copyright (c) 2004, 2011 IBM Corporation
 * All rights reserved.
 * This program and the accompanying materials
 * are made available under the terms of the BSD License
 * which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * Contributors:
 *     IBM Corporation - initial implementation
 *****************************************************************************/

#include <virtio.h>
#include <virtio-blk.h>
#include <virtio-9p.h>

/******** core virtio ********/

// : virtio-vring-size  ( queuesize -- ringsize )
PRIM(virtio_X2d_vring_X2d_size)
	TOS.u = virtio_vring_size(TOS.u);
MIRP

// : virtio-get-qsize  ( dev queue -- queuesize )
PRIM(virtio_X2d_get_X2d_qsize)
	int queue = TOS.u; POP;
	TOS.u = virtio_get_qsize(TOS.a, queue);
MIRP

// : virtio-get-config  ( dev offset size -- val )
PRIM(virtio_X2d_get_X2d_config)
	int size = TOS.u; POP;
	int offset = TOS.u; POP;
	TOS.u = virtio_get_config(TOS.a, offset, size);
MIRP

/******** virtio-blk ********/

// : virtio-blk-init ( dev -- )
PRIM(virtio_X2d_blk_X2d_init)
	void *dev = TOS.a; POP;
	virtioblk_init(dev);
MIRP

// : virtio-blk-shutdown ( dev -- )
PRIM(virtio_X2d_blk_X2d_shutdown)
	void *dev = TOS.a; POP;
	virtioblk_shutdown(dev);
MIRP

// : virtio-blk-read ( dev blkno cnt reg -- #read )
PRIM(virtio_X2d_blk_X2d_read)
	void *dev = TOS.a; POP;
	long cnt = TOS.n; POP;
	long blkno = TOS.n; POP;
	void *buf = TOS.a;
	TOS.n = virtioblk_read(dev, buf, blkno, cnt);
MIRP

/******** virtio-fs ********/

// : virtio-fs-init ( dev tx rx size -- success )
PRIM(virtio_X2d_fs_X2d_init)
	int size = TOS.n; POP;
	void *rx = TOS.a; POP;
	void *tx = TOS.a; POP;
	void *dev = TOS.a;

	TOS.n = virtio_9p_init(dev, tx, rx, size) == 0 ? -1 : 0;
MIRP

// : virtio-fs-shutdown ( dev -- )
PRIM(virtio_X2d_fs_X2d_shutdown)
	void *dev = TOS.a; POP;

	virtio_9p_shutdown(dev);
MIRP

// : virtio-fs-load ( dev buf str -- #read )
PRIM(virtio_X2d_fs_X2d_load)
	char *str = TOS.a; POP;
	void *buf = TOS.a; POP;
	void *dev = TOS.a;

	TOS.n = virtio_9p_load(dev, str, buf);
MIRP
