DrvSCSI.cpp revision 26947320577c481b4afefdb0afbb855181e5b2e8
/* $Id$ */
/** @file
* VBox storage drivers: Generic SCSI command parser and execution driver
*/
/*
* Copyright (C) 2006-2010 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
//#define DEBUG
#define LOG_GROUP LOG_GROUP_DRV_SCSI
#include <VBox/pdmthread.h>
#include <iprt/semaphore.h>
#include "Builtins.h"
/**
* SCSI driver instance data.
*
* @implements PDMISCSICONNECTOR
* @implements PDMIBLOCKASYNCPORT
* @implements PDMIMOUNTNOTIFY
*/
typedef struct DRVSCSI
{
/** Pointer driver instance. */
/** Pointer to the attached driver's base interface. */
/** Pointer to the attached driver's block interface. */
/** Pointer to the attached driver's async block interface. */
/** Pointer to the attached driver's block bios interface. */
/** Pointer to the attached driver's mount interface. */
/** Pointer to the SCSI port interface of the device above. */
/** pointer to the Led port interface of the dveice above. */
/** The scsi connector interface .*/
/** The block port interface. */
/** The optional block async port interface. */
#if 0 /* these interfaces aren't implemented */
/** The mount notify interface. */
#endif
/** Fallback status LED state for this drive.
* This is used in case the device doesn't has a LED interface. */
/** Pointer to the status LED for this drive. */
/** VSCSI device handle. */
/** VSCSI LUN handle. */
/** I/O callbacks. */
/** The dedicated I/O thread for the non async approach. */
/** Queue for passing the requests to the thread. */
/** Request that we've left pending on wakeup or reset. */
/** Indicates whether PDMDrvHlpAsyncNotificationCompleted should be called by
* any of the dummy functions. */
bool volatile fDummySignal;
/** Release statistics: number of bytes written. */
/** Release statistics: number of bytes read. */
/** Release statistics: Current I/O depth. */
volatile uint32_t StatIoDepth;
/** Converts a pointer to DRVSCSI::ISCSIConnector to a PDRVSCSI. */
#define PDMISCSICONNECTOR_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, ISCSIConnector)) )
/** Converts a pointer to DRVSCSI::IPortAsync to a PDRVSCSI. */
#define PDMIBLOCKASYNCPORT_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, IPortAsync)) )
{
int rc = VINF_SUCCESS;
switch (enmTxDir)
{
case VSCSIIOREQTXDIR_FLUSH:
{
break;
}
case VSCSIIOREQTXDIR_READ:
case VSCSIIOREQTXDIR_WRITE:
{
size_t cbTransfer = 0;
unsigned cSeg = 0;
&paSeg);
while (cbTransfer && cSeg)
{
if (enmTxDir == VSCSIIOREQTXDIR_READ)
{
if (RT_FAILURE(rc))
}
else
{
if (RT_FAILURE(rc))
}
/* Go to the next entry. */
cbTransfer -= cbProcess;
paSeg++;
cSeg--;
}
break;
}
default:
}
return VINF_SUCCESS;
}
{
return VINF_SUCCESS;
}
{
if (enmTxDir == VSCSIIOREQTXDIR_READ)
else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
else
return VINF_SUCCESS;
}
void *pvScsiLunUser,
{
int rc = VINF_SUCCESS;
if (pThis->pDrvBlockAsync)
{
/* asnyc I/O path. */
switch (enmTxDir)
{
case VSCSIIOREQTXDIR_FLUSH:
{
/** @todo Flush callback for the async I/O interface. */
break;
}
case VSCSIIOREQTXDIR_READ:
case VSCSIIOREQTXDIR_WRITE:
{
size_t cbTransfer = 0;
unsigned cSeg = 0;
if (enmTxDir == VSCSIIOREQTXDIR_READ)
{
if (RT_FAILURE(rc))
}
else
{
if (RT_FAILURE(rc))
}
break;
}
default:
}
}
else
{
/* I/O thread. */
}
return rc;
}
void *pVScsiReqUser, int rcReq)
{
rcReq);
}
/**
* Dummy request function used by drvscsiReset to wait for all pending requests
* to complete prior to the device reset.
*
* @param pThis Pointer to the instace data.
* @returns VINF_SUCCESS.
*/
{
if (pThis->fDummySignal)
return VINF_SUCCESS;
}
/**
* Request function to wakeup the thread.
*
* @param pThis Pointer to the instace data.
* @returns VWRN_STATE_CHANGED.
*/
{
if (pThis->fDummySignal)
return VWRN_STATE_CHANGED;
}
/**
* The thread function which processes the requests asynchronously.
*
* @returns VBox status code.
* @param pDrvIns Pointer to the driver instance data.
* @param pThread Pointer to the thread instance data.
*/
{
int rc = VINF_SUCCESS;
LogFlowFunc(("Entering async IO loop.\n"));
return VINF_SUCCESS;
{
AssertMsg(rc == VWRN_STATE_CHANGED, ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n", rc));
}
return VINF_SUCCESS;
}
/**
* Deals with any pending dummy request
*
* @returns true if no pending dummy request, false if still pending.
* @param pThis The instance data.
* @param cMillies The number of milliseconds to wait for any
* pending request to finish.
*/
{
if (!pThis->pPendingDummyReq)
return true;
if (RT_FAILURE(rc))
return false;
return true;
}
{
int rc;
{
LogRel(("drvscsiAsyncIOLoopWakeup#%u: previous dummy request is still pending\n", pDrvIns->iInstance));
return VERR_TIMEOUT;
}
rc = RTReqCall(pThis->pQueueRequests, &pReq, 10000 /* 10 sec. */, (PFNRT)drvscsiAsyncIOLoopWakeupFunc, 1, pThis);
if (RT_SUCCESS(rc))
else
{
}
return rc;
}
/* -=-=-=-=- ISCSIConnector -=-=-=-=- */
/** @copydoc PDMISCSICONNECTOR::pfnSCSIRequestSend. */
static DECLCALLBACK(int) drvscsiRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
{
int rc;
if (RT_FAILURE(rc))
return rc;
return rc;
}
/* -=-=-=-=- IBase -=-=-=-=- */
/**
* @interface_method_impl{PDMIBASE,pfnQueryInterface}
*/
{
return NULL;
}
/**
* Worker for drvscsiReset, drvscsiSuspend and drvscsiPowerOff.
*
* @param pDrvIns The driver instance.
* @param pfnAsyncNotify The async callback.
*/
static void drvscsiR3ResetOrSuspendOrPowerOff(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
{
if (!pThis->pQueueRequests)
return;
{
{
return;
}
int rc = RTReqCall(pThis->pQueueRequests, &pReq, 0 /*ms*/, (PFNRT)drvscsiAsyncIOLoopSyncCallback, 1, pThis);
if (RT_SUCCESS(rc))
{
return;
}
}
}
/**
* Callback employed by drvscsiSuspend and drvscsiPowerOff.
*
* @returns true if we've quiesced, false if we're still working.
* @param pDrvIns The driver instance.
*/
{
return false;
return true;
}
/**
* @copydoc FNPDMDRVPOWEROFF
*/
{
}
/**
* @copydoc FNPDMDRVSUSPEND
*/
{
}
/**
* Callback employed by drvscsiReset.
*
* @returns true if we've quiesced, false if we're still working.
* @param pDrvIns The driver instance.
*/
{
return false;
return true;
}
/**
* @copydoc FNPDMDRVRESET
*/
{
}
/**
* Destruct a driver instance.
*
* Most VM resources are freed by the VM. This callback is provided so that any non-VM
* resources can be freed correctly.
*
* @param pDrvIns The driver instance data.
*/
{
if (pThis->pQueueRequests)
{
}
/* Free the VSCSI device and LUN handle. */
}
/**
* Construct a block driver instance.
*
* @copydoc FNPDMDRVCONSTRUCT
*/
{
/*
* Initialize the instance data.
*/
/*
* Try attach driver below and query it's block interface.
*/
/*
* Query the block and blockbios interfaces.
*/
{
AssertMsgFailed(("Configuration error: No block interface!\n"));
return VERR_PDM_MISSING_INTERFACE;
}
if (!pThis->pDrvBlockBios)
{
AssertMsgFailed(("Configuration error: No block BIOS interface!\n"));
return VERR_PDM_MISSING_INTERFACE;
}
/* Query the SCSI port interface above. */
AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
/* Query the optional LED interface above. */
{
/* Get The Led. */
if (RT_FAILURE(rc))
}
else
/* Try to get the optional async block interface. */
if (enmType != PDMBLOCKTYPE_HARD_DISK)
N_("Only hard disks are currently supported as SCSI devices (enmType=%d)"),
enmType);
/* Create VSCSI device and LUN. */
pThis);
/* Create request queue. */
/* Register statistics counter. */
/** @todo aeichner: Find a way to put the instance number of the attached
* controller device when we support more than one controller of the same type.
* At the moment we have the 0 hardcoded. */
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
pThis->StatIoDepth = 0;
PDMDrvHlpSTAMRegisterF(pDrvIns, (void *)&pThis->StatIoDepth, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
/* Create I/O thread. */
return VINF_SUCCESS;
}
/**
* SCSI driver registration record.
*/
{
/* u32Version */
/* szName */
"SCSI",
/* szRCMod */
"",
/* szR0Mod */
"",
/* pszDescription */
"Generic SCSI driver.",
/* fFlags */
/* fClass. */
/* cMaxInstances */
~0,
/* cbInstance */
sizeof(DRVSCSI),
/* pfnConstruct */
/* pfnDestruct */
/* pfnRelocate */
NULL,
/* pfnIOCtl */
NULL,
/* pfnPowerOn */
NULL,
/* pfnReset */
/* pfnSuspend */
/* pfnResume */
NULL,
/* pfnAttach */
NULL,
/* pfnDetach */
NULL,
/* pfnPowerOff */
/* pfnSoftReset */
NULL,
/* u32EndVersion */
};