Virtio-solaris.c revision 38b0f04a36431d57eed1de01653dd0a015eae4c3
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/* $Id$ */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/** @file
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * VirtualBox Guest Additions: Virtio Driver for Solaris.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/*
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Copyright (C) 2010 Oracle Corporation
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Oracle Corporation confidential
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * All rights reserved
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/*******************************************************************************
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync* Header Files *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync*******************************************************************************/
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#include "Virtio-solaris.h"
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#include <iprt/assert.h>
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#include <iprt/mem.h>
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#include <VBox/log.h>
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#if defined(DEBUG_ramshankar)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# undef LogFlowFunc
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# define LogFlowFunc LogRel
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# undef Log
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# define Log LogRel
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# undef LogFlow
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync# define LogFlow LogRel
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync#endif
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/**
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Virtio Attach routine that should be called from all Virtio drivers' attach
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * routines.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pDip The module structure instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param enmCmd Operation type (attach/resume).
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pDeviceOps Pointer to device ops structure.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pHyperOps Pointer to hypervisor ops structure.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @return Solaris DDI error code. DDI_SUCCESS or DDI_FAILURE.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsyncint VirtioAttach(dev_info_t *pDip, ddi_attach_cmd_t enmCmd, PVIRTIODEVICEOPS pDeviceOps, PVIRTIOHYPEROPS pHyperOps)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync{
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogFlowFunc((VIRTIOLOGNAME ":VirtioAttach: pDip=%p enmCmd=%d pDeviceOps=%p pHyperOps=%p\n", pDip, enmCmd, pDeviceOps, pHyperOps));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturn(pDip, DDI_EINVAL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturn(pDeviceOps, DDI_EINVAL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturn(pHyperOps, DDI_EINVAL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (enmCmd != DDI_ATTACH)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: Invalid enmCmd=%#x expected DDI_ATTACH\n", enmCmd));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync int rc = DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync PVIRTIODEVICE pDevice = RTMemAllocZ(sizeof(VIRTIODEVICE));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_LIKELY(pDevice))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pDip = pDip;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pDeviceOps = pDeviceOps;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps = pHyperOps;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pvDevice = pDevice->pDeviceOps->pfnAlloc(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_LIKELY(pDevice->pvDevice))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pvHyper = pDevice->pHyperOps->pfnAlloc(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_LIKELY(pDevice->pvHyper))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync /*
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Attach hypervisor interface and obtain features supported by host.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync rc = pDevice->pHyperOps->pfnAttach(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (rc == DDI_SUCCESS)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->fHostFeatures = pDevice->pHyperOps->pfnGetFeatures(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogFlow((VIRTIOLOGNAME ":VirtioAttach: Host features=%#x\n", pDevice->fHostFeatures));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync /*
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Attach the device type interface.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync rc = pDevice->pDeviceOps->pfnAttach(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (rc == DDI_SUCCESS)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync ddi_set_driver_private(pDip, pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_SUCCESS;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: DeviceOps pfnAttach failed. rc=%d\n", rc));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps->pfnDetach(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: HyperOps pfnAttach failed. rc=%d\n", rc));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps->pfnFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: HyperOps->pfnAlloc failed!\n"));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pDeviceOps->pfnFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: DeviceOps->pfnAlloc failed!\n"));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync RTMemFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioAttach: failed to alloc %u bytes for device structure.\n", sizeof(VIRTIODEVICE)));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync}
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/**
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Virtio Detach routine that should be called from all Virtio drivers' detach
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * routines.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pDip The module structure instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param enmCmd Operation type (detach/suspend).
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @return Solaris DDI error code. DDI_SUCCESS or DDI_FAILURE.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsyncint VirtioDetach(dev_info_t *pDip, ddi_detach_cmd_t enmCmd)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync{
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogFlowFunc((VIRTIOLOGNAME ":VirtioDetach pDip=%p enmCmd=%d\n", pDip, enmCmd));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync PVIRTIODEVICE pDevice = ddi_get_driver_private(pDip);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_UNLIKELY(!pDevice))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (enmCmd != DDI_DETACH)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioDetach: Invalid enmCmd=%#x expected DDI_DETACH.\n", enmCmd));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync int rc = pDevice->pDeviceOps->pfnDetach(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (rc == DDI_SUCCESS)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps->pfnDetach(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pDeviceOps->pfnFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pvDevice = NULL;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps->pfnFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pvHyper = NULL;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync ddi_set_driver_private(pDevice->pDip, NULL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync RTMemFree(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_SUCCESS;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync else
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioDetach: DeviceOps pfnDetach failed. rc=%d\n", rc));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return DDI_FAILURE;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync}
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/**
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Allocates a Virtio Queue object and assigns it an index.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pDevice Pointer to the Virtio device instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @return A pointer to a Virtio Queue instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsyncPVIRTIOQUEUE VirtioGetQueue(PVIRTIODEVICE pDevice, uint16_t Index)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync{
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync PVIRTIOQUEUE pQueue = RTMemAllocZ(sizeof(VIRTIOQUEUE));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_UNLIKELY(!pQueue))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioGetQueue: failed to alloc memory for %u bytes.\n", sizeof(VIRTIOQUEUE)));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return NULL;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pQueue->QueueIndex = Index;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pQueue->pvData = pDevice->pHyperOps->pfnGetQueue(pDevice, pQueue);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync if (RT_UNLIKELY(!pQueue->pvData))
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync {
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync LogRel((VIRTIOLOGNAME ":VirtioGetQueue: HyperOps GetQueue failed!\n"));
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync RTMemFree(pQueue);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return NULL;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync }
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturn(pQueue->pQueue, NULL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturn(pQueue->Ring.cDesc > 0, NULL);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync /* @todo enable interrupt. */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync return pQueue;
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync}
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync/**
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * Puts a queue and destroys the instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync *
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pDevice Pointer to the Virtio device instance.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync * @param pQueue Pointer to the Virtio queue.
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync */
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsyncvoid VirtioPutQueue(PVIRTIODEVICE pDevice, PVIRTIOQUEUE pQueue)
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync{
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturnVoid(pDevice);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync AssertReturnVoid(pQueue);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync pDevice->pHyperOps->pfnPutQueue(pDevice, pQueue);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync RTMemFree(pQueue);
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync}
38b0f04a36431d57eed1de01653dd0a015eae4c3vboxsync