DrvSCSI.cpp revision ead016c68c61b5f2e1fe4d237054eebea9327d4b
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* $Id$ */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @file
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * VBox storage drivers: Generic SCSI command parser and execution driver
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2006-2010 Oracle Corporation
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync//#define DEBUG
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define LOG_GROUP LOG_GROUP_DRV_SCSI
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/pdmdrv.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/pdmifs.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/pdmthread.h>
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#include <VBox/vscsi.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/assert.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <iprt/mem.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/req.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/semaphore.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <iprt/string.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include <iprt/uuid.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include "Builtins.h"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * SCSI driver instance data.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync *
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * @implements PDMISCSICONNECTOR
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * @implements PDMIBLOCKASYNCPORT
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * @implements PDMIMOUNTNOTIFY
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsynctypedef struct DRVSCSI
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer driver instance. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMDRVINS pDrvIns;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the attached driver's base interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMIBASE pDrvBase;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the attached driver's block interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMIBLOCK pDrvBlock;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the attached driver's async block interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMIBLOCKASYNC pDrvBlockAsync;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the attached driver's block bios interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMIBLOCKBIOS pDrvBlockBios;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the attached driver's mount interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMIMOUNT pDrvMount;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Pointer to the SCSI port interface of the device above. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMISCSIPORT pDevScsiPort;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** pointer to the Led port interface of the dveice above. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMILEDPORTS pLedPort;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The scsi connector interface .*/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMISCSICONNECTOR ISCSIConnector;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The block port interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMIBLOCKPORT IPort;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The optional block async port interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMIBLOCKASYNCPORT IPortAsync;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#if 0 /* these interfaces aren't implemented */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The mount notify interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMIMOUNTNOTIFY IMountNotify;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#endif
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** Fallback status LED state for this drive.
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * This is used in case the device doesn't has a LED interface. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMLED Led;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync /** Pointer to the status LED for this drive. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMLED pLed;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** VSCSI device handle. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIDEVICE hVScsiDevice;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** VSCSI LUN handle. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSILUN hVScsiLun;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /** I/O callbacks. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSILUNIOCALLBACKS VScsiIoCallbacks;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** The dedicated I/O thread for the non async approach. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMTHREAD pAsyncIOThread;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Queue for passing the requests to the thread. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PRTREQQUEUE pQueueRequests;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync /** Request that we've left pending on wakeup or reset. */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync PRTREQ pPendingDummyReq;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /** Indicates whether PDMDrvHlpAsyncNotificationCompleted should be called by
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * any of the dummy functions. */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync bool volatile fDummySignal;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Release statistics: number of bytes written. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync STAMCOUNTER StatBytesWritten;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /** Release statistics: number of bytes read. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync STAMCOUNTER StatBytesRead;
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync /** Release statistics: Current I/O depth. */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync volatile uint32_t StatIoDepth;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync} DRVSCSI, *PDRVSCSI;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Converts a pointer to DRVSCSI::ISCSIConnector to a PDRVSCSI. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#define PDMISCSICONNECTOR_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, ISCSIConnector)) )
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** Converts a pointer to DRVSCSI::IPortAsync to a PDRVSCSI. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#define PDMIBLOCKASYNCPORT_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, IPortAsync)) )
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic int drvscsiProcessRequestOne(PDRVSCSI pThis, VSCSIIOREQ hVScsiIoReq)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VINF_SUCCESS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIOREQTXDIR enmTxDir;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync switch (enmTxDir)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_FLUSH:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pThis->pDrvBlock->pfnFlush(pThis->pDrvBlock);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_READ:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_WRITE:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync uint64_t uOffset = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync size_t cbTransfer = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync size_t cbSeg = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync PCRTSGSEG paSeg = NULL;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync unsigned cSeg = 0;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer, &cSeg, &cbSeg,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &paSeg);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync while (cbTransfer && cSeg)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync size_t cbProcess = (cbTransfer < paSeg->cbSeg) ? cbTransfer : paSeg->cbSeg;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync Log(("%s: uOffset=%llu cbProcess=%u\n", __FUNCTION__, uOffset, cbProcess));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (enmTxDir == VSCSIIOREQTXDIR_READ)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pThis->pDrvBlock->pfnRead(pThis->pDrvBlock, uOffset,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync paSeg->pvSeg, cbProcess);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Actual.s.fReading = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_FAILURE(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("%s: Failed to read data %Rrc\n", __FUNCTION__, rc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbProcess);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pThis->pDrvBlock->pfnWrite(pThis->pDrvBlock, uOffset,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync paSeg->pvSeg, cbProcess);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Actual.s.fWriting = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_FAILURE(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("%s: Failed to write data %Rrc\n", __FUNCTION__, rc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbProcess);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Go to the next entry. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uOffset += cbProcess;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync cbTransfer -= cbProcess;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync paSeg++;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync cSeg--;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync break;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync default:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("Invalid transfer direction %d\n", enmTxDir));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync ASMAtomicDecU32(&pThis->StatIoDepth);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIoReqCompleted(hVScsiIoReq, rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic int drvscsiGetSize(VSCSILUN hVScsiLun, void *pvScsiLunUser, uint64_t *pcbSize)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *pcbSize = pThis->pDrvBlock->pfnGetSize(pThis->pDrvBlock);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsyncstatic int drvscsiTransferCompleteNotify(PPDMIBLOCKASYNCPORT pInterface, void *pvUser, int rc)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PDRVSCSI pThis = PDMIBLOCKASYNCPORT_2_DRVSCSI(pInterface);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIOREQ hVScsiIoReq = (VSCSIIOREQ)pvUser;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIOREQTXDIR enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync LogFlowFunc(("Request hVScsiIoReq=%#p completed\n", hVScsiIoReq));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (enmTxDir == VSCSIIOREQTXDIR_READ)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Actual.s.fReading = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Actual.s.fWriting = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ead016c68c61b5f2e1fe4d237054eebea9327d4bvboxsync AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync ASMAtomicDecU32(&pThis->StatIoDepth);
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsync VSCSIIoReqCompleted(hVScsiIoReq, rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic int drvscsiReqTransferEnqueue(VSCSILUN hVScsiLun,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync void *pvScsiLunUser,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIOREQ hVScsiIoReq)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VINF_SUCCESS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync ASMAtomicIncU32(&pThis->StatIoDepth);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (pThis->pDrvBlockAsync)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /* async I/O path. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIIOREQTXDIR enmTxDir;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync LogFlowFunc(("Enqueuing hVScsiIoReq=%#p\n", hVScsiIoReq));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync switch (enmTxDir)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_FLUSH:
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = pThis->pDrvBlockAsync->pfnStartFlush(pThis->pDrvBlockAsync, hVScsiIoReq);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertMsgFailed(("%s: Failed to flush data %Rrc\n", __FUNCTION__, rc));
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_READ:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync case VSCSIIOREQTXDIR_WRITE:
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync uint64_t uOffset = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync size_t cbTransfer = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync size_t cbSeg = 0;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync PCRTSGSEG paSeg = NULL;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync unsigned cSeg = 0;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &cSeg, &cbSeg, &paSeg);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (enmTxDir == VSCSIIOREQTXDIR_READ)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pThis->pDrvBlockAsync->pfnStartRead(pThis->pDrvBlockAsync, uOffset,
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync paSeg, cSeg, cbTransfer,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync hVScsiIoReq);
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("%s: Failed to read data %Rrc\n", __FUNCTION__, rc));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbTransfer);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = pThis->pDrvBlockAsync->pfnStartWrite(pThis->pDrvBlockAsync, uOffset,
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync paSeg, cSeg, cbTransfer,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync hVScsiIoReq);
d5ea45cc92d7f1d3ade8189944531f665bfe8ed5vboxsync if (RT_FAILURE(rc) && rc != VERR_VD_ASYNC_IO_IN_PROGRESS)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("%s: Failed to write data %Rrc\n", __FUNCTION__, rc));
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbTransfer);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync }
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync default:
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgFailed(("Invalid transfer direction %u\n", enmTxDir));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (rc == VINF_VD_ASYNC_IO_FINISHED)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (enmTxDir == VSCSIIOREQTXDIR_READ)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync pThis->pLed->Actual.s.fReading = 0;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync pThis->pLed->Actual.s.fWriting = 0;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync ASMAtomicDecU32(&pThis->StatIoDepth);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync VSCSIIoReqCompleted(hVScsiIoReq, VINF_SUCCESS);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync }
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = VINF_SUCCESS;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else if (RT_FAILURE(rc))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync if (enmTxDir == VSCSIIOREQTXDIR_READ)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync pThis->pLed->Actual.s.fReading = 0;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync pThis->pLed->Actual.s.fWriting = 0;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync ASMAtomicDecU32(&pThis->StatIoDepth);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync VSCSIIoReqCompleted(hVScsiIoReq, rc);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync }
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync else
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertMsgFailed(("Invalid return code rc=%Rrc\n", rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* I/O thread. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTReqCallEx(pThis->pQueueRequests, NULL, 0, RTREQFLAGS_NO_WAIT,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync (PFNRT)drvscsiProcessRequestOne, 2, pThis, hVScsiIoReq);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncstatic void drvscsiVScsiReqCompleted(VSCSIDEVICE hVScsiDevice, void *pVScsiDeviceUser,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync void *pVScsiReqUser, int rcReq)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PDRVSCSI pThis = (PDRVSCSI)pVScsiDeviceUser;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->pDevScsiPort->pfnSCSIRequestCompleted(pThis->pDevScsiPort, (PPDMSCSIREQUEST)pVScsiReqUser,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rcReq);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync/**
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Dummy request function used by drvscsiReset to wait for all pending requests
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * to complete prior to the device reset.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pThis Pointer to the instace data.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync * @returns VINF_SUCCESS.
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic int drvscsiAsyncIOLoopSyncCallback(PDRVSCSI pThis)
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (pThis->fDummySignal)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync return VINF_SUCCESS;
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync}
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Request function to wakeup the thread.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pThis Pointer to the instace data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VWRN_STATE_CHANGED.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic int drvscsiAsyncIOLoopWakeupFunc(PDRVSCSI pThis)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (pThis->fDummySignal)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VWRN_STATE_CHANGED;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * The thread function which processes the requests asynchronously.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @returns VBox status code.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pDrvIns Pointer to the driver instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pThread Pointer to the thread instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int drvscsiAsyncIOLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync LogFlowFunc(("Entering async IO loop.\n"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync while (pThread->enmState == PDMTHREADSTATE_RUNNING)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTReqProcess(pThis->pQueueRequests, RT_INDEFINITE_WAIT);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsg(rc == VWRN_STATE_CHANGED, ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n", rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync/**
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Deals with any pending dummy request
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @returns true if no pending dummy request, false if still pending.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param pThis The instance data.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * @param cMillies The number of milliseconds to wait for any
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * pending request to finish.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsyncstatic bool drvscsiAsyncIOLoopNoPendingDummy(PDRVSCSI pThis, uint32_t cMillies)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync{
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (!pThis->pPendingDummyReq)
8a339f91959bb7a3315b51a23461b68c7b0cb50evboxsync return true;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = RTReqWait(pThis->pPendingDummyReq, cMillies);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (RT_FAILURE(rc))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return false;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync RTReqFree(pThis->pPendingDummyReq);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync pThis->pPendingDummyReq = NULL;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return true;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync}
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int drvscsiAsyncIOLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PRTREQ pReq;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 10000 /* 10 sec */))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync LogRel(("drvscsiAsyncIOLoopWakeup#%u: previous dummy request is still pending\n", pDrvIns->iInstance));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return VERR_TIMEOUT;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync rc = RTReqCall(pThis->pQueueRequests, &pReq, 10000 /* 10 sec. */, (PFNRT)drvscsiAsyncIOLoopWakeupFunc, 1, pThis);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync if (RT_SUCCESS(rc))
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync RTReqFree(pReq);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync pThis->pPendingDummyReq = pReq;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync LogRel(("drvscsiAsyncIOLoopWakeup#%u: %Rrc pReq=%p\n", pDrvIns->iInstance, rc, pReq));
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* -=-=-=-=- ISCSIConnector -=-=-=-=- */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @copydoc PDMISCSICONNECTOR::pfnSCSIRequestSend. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(int) drvscsiRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDRVSCSI pThis = PDMISCSICONNECTOR_2_DRVSCSI(pInterface);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VSCSIREQ hVScsiReq;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIDeviceReqCreate(pThis->hVScsiDevice, &hVScsiReq,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->uLogicalUnit,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->pbCDB,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->cbCDB,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->cbScatterGather,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->cScatterGatherEntries,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->paScatterGatherHead,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->pbSenseBuffer,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest->cbSenseBuffer,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pSCSIRequest);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_FAILURE(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIDeviceReqEnqueue(pThis->hVScsiDevice, hVScsiReq);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* -=-=-=-=- IBase -=-=-=-=- */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/**
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * @interface_method_impl{PDMIBASE,pfnQueryInterface}
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncstatic DECLCALLBACK(void *) drvscsiQueryInterface(PPDMIBASE pInterface, const char *pszIID)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSICONNECTOR, &pThis->ISCSIConnector);
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBLOCKPORT, &pThis->IPort);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBLOCKASYNCPORT, &pThis->IPortAsync);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync return NULL;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync/**
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * Worker for drvscsiReset, drvscsiSuspend and drvscsiPowerOff.
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pDrvIns The driver instance.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pfnAsyncNotify The async callback.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic void drvscsiR3ResetOrSuspendOrPowerOff(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (!pThis->pQueueRequests)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync ASMAtomicWriteBool(&pThis->fDummySignal, true);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (!RTReqIsBusy(pThis->pQueueRequests))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync ASMAtomicWriteBool(&pThis->fDummySignal, false);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PRTREQ pReq;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync int rc = RTReqCall(pThis->pQueueRequests, &pReq, 0 /*ms*/, (PFNRT)drvscsiAsyncIOLoopSyncCallback, 1, pThis);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (RT_SUCCESS(rc))
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync ASMAtomicWriteBool(&pThis->fDummySignal, false);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync RTReqFree(pReq);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pThis->pPendingDummyReq = pReq;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDMDrvHlpSetAsyncNotification(pDrvIns, pfnAsyncNotify);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync}
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync/**
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Callback employed by drvscsiSuspend and drvscsiPowerOff.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @returns true if we've quiesced, false if we're still working.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pDrvIns The driver instance.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic DECLCALLBACK(bool) drvscsiIsAsyncSuspendOrPowerOffDone(PPDMDRVINS pDrvIns)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return false;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync ASMAtomicWriteBool(&pThis->fDummySignal, false);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PDMR3ThreadSuspend(pThis->pAsyncIOThread);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return true;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync}
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync/**
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @copydoc FNPDMDRVPOWEROFF
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsyncstatic DECLCALLBACK(void) drvscsiPowerOff(PPDMDRVINS pDrvIns)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync}
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync/**
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @copydoc FNPDMDRVSUSPEND
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsyncstatic DECLCALLBACK(void) drvscsiSuspend(PPDMDRVINS pDrvIns)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync}
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync/**
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Callback employed by drvscsiReset.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @returns true if we've quiesced, false if we're still working.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pDrvIns The driver instance.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic DECLCALLBACK(bool) drvscsiIsAsyncResetDone(PPDMDRVINS pDrvIns)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync{
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return false;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync ASMAtomicWriteBool(&pThis->fDummySignal, false);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return true;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync}
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync/**
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * @copydoc FNPDMDRVRESET
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsyncstatic DECLCALLBACK(void) drvscsiReset(PPDMDRVINS pDrvIns)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncResetDone);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync}
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Destruct a driver instance.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Most VM resources are freed by the VM. This callback is provided so that any non-VM
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * resources can be freed correctly.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param pDrvIns The driver instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(void) drvscsiDestruct(PPDMDRVINS pDrvIns)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pThis->pQueueRequests)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 100 /*ms*/))
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync LogRel(("drvscsiDestruct#%u: previous dummy request is still pending\n", pDrvIns->iInstance));
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync int rc = RTReqDestroyQueue(pThis->pQueueRequests);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgRC(rc, ("Failed to destroy queue rc=%Rrc\n", rc));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync /* Free the VSCSI device and LUN handle. */
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync VSCSILUN hVScsiLun;
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync int rc = VSCSIDeviceLunDetach(pThis->hVScsiDevice, 0, &hVScsiLun);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync AssertRC(rc);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync Assert(hVScsiLun == pThis->hVScsiLun);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync rc = VSCSILunDestroy(hVScsiLun);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync AssertRC(rc);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync rc = VSCSIDeviceDestroy(pThis->hVScsiDevice);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Construct a block driver instance.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
cba6719bd64ec749967bbe931230452664109857vboxsync * @copydoc FNPDMDRVCONSTRUCT
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsyncstatic DECLCALLBACK(int) drvscsiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync LogFlowFunc(("pDrvIns=%#p pCfg=%#p\n", pDrvIns, pCfg));
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync * Initialize the instance data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync pThis->pDrvIns = pDrvIns;
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync pThis->ISCSIConnector.pfnSCSIRequestSend = drvscsiRequestSend;
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync pDrvIns->IBase.pfnQueryInterface = drvscsiQueryInterface;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->IPortAsync.pfnTransferCompleteNotify = drvscsiTransferCompleteNotify;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Try attach driver below and query it's block interface.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
cba6719bd64ec749967bbe931230452664109857vboxsync int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pThis->pDrvBase);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Attaching driver below failed rc=%Rrc\n", rc), rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Query the block and blockbios interfaces.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync pThis->pDrvBlock = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCK);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (!pThis->pDrvBlock)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgFailed(("Configuration error: No block interface!\n"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VERR_PDM_MISSING_INTERFACE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync pThis->pDrvBlockBios = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCKBIOS);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (!pThis->pDrvBlockBios)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgFailed(("Configuration error: No block BIOS interface!\n"));
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VERR_PDM_MISSING_INTERFACE;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Query the SCSI port interface above. */
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync pThis->pDevScsiPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISCSIPORT);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync pThis->pDrvMount = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMOUNT);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Query the optional LED interface above. */
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync pThis->pLedPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMILEDPORTS);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pThis->pLedPort != NULL)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Get The Led. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = pThis->pLedPort->pfnQueryStatusLed(pThis->pLedPort, 0, &pThis->pLed);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_FAILURE(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pThis->pLed = &pThis->Led;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pThis->pLed = &pThis->Led;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Try to get the optional async block interface. */
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync pThis->pDrvBlockAsync = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCKASYNC);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
cba6719bd64ec749967bbe931230452664109857vboxsync PDMBLOCKTYPE enmType = pThis->pDrvBlock->pfnGetType(pThis->pDrvBlock);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (enmType != PDMBLOCKTYPE_HARD_DISK)
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_UNSUPPORTED_BLOCK_TYPE, RT_SRC_POS,
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync N_("Only hard disks are currently supported as SCSI devices (enmType=%d)"),
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync enmType);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Create VSCSI device and LUN. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->VScsiIoCallbacks.pfnVScsiLunMediumGetSize = drvscsiGetSize;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis->VScsiIoCallbacks.pfnVScsiLunReqTransferEnqueue = drvscsiReqTransferEnqueue;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIDeviceCreate(&pThis->hVScsiDevice, drvscsiVScsiReqCompleted, pThis);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI device rc=%Rrc\n"), rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSILunCreate(&pThis->hVScsiLun, VSCSILUNTYPE_SBC, &pThis->VScsiIoCallbacks,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThis);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI LUN rc=%Rrc\n"), rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VSCSIDeviceLunAttach(pThis->hVScsiDevice, pThis->hVScsiLun, 0);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Failed to attached the LUN to the SCSI device\n"), rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Create request queue. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTReqCreateQueue(&pThis->pQueueRequests);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create request queue rc=%Rrc\n"), rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Register statistics counter. */
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync /** @todo aeichner: Find a way to put the instance number of the attached
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * controller device when we support more than one controller of the same type.
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * At the moment we have the 0 hardcoded. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync "Amount of data read.", "/Devices/SCSI0/%d/ReadBytes", pDrvIns->iInstance);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync "Amount of data written.", "/Devices/SCSI0/%d/WrittenBytes", pDrvIns->iInstance);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync pThis->StatIoDepth = 0;
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync PDMDrvHlpSTAMRegisterF(pDrvIns, (void *)&pThis->StatIoDepth, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync "Number of active tasks.", "/Devices/SCSI0/%d/IoDepth", pDrvIns->iInstance);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Create I/O thread. */
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pAsyncIOThread, pThis, drvscsiAsyncIOLoop,
fbf08fabb4c4b383d6aa2830c2bd5b943a26f10cvboxsync drvscsiAsyncIOLoopWakeup, 0, RTTHREADTYPE_IO, "SCSI async IO");
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create async I/O thread rc=%Rrc\n"), rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * SCSI driver registration record.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncconst PDMDRVREG g_DrvSCSI =
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* u32Version */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDM_DRVREG_VERSION,
5b465a7c1237993faf8bb50120d247f3f0319adavboxsync /* szName */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync "SCSI",
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync /* szRCMod */
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync "",
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync /* szR0Mod */
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync "",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pszDescription */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync "Generic SCSI driver.",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* fFlags */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* fClass. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PDM_DRVREG_CLASS_SCSI,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* cMaxInstances */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync ~0,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* cbInstance */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync sizeof(DRVSCSI),
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnConstruct */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync drvscsiConstruct,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnDestruct */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync drvscsiDestruct,
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync /* pfnRelocate */
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnIOCtl */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnPowerOn */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnReset */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync drvscsiReset,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnSuspend */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync drvscsiSuspend,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnResume */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync NULL,
cba6719bd64ec749967bbe931230452664109857vboxsync /* pfnAttach */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync NULL,
cba6719bd64ec749967bbe931230452664109857vboxsync /* pfnDetach */
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync NULL,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* pfnPowerOff */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync drvscsiPowerOff,
cba6719bd64ec749967bbe931230452664109857vboxsync /* pfnSoftReset */
cba6719bd64ec749967bbe931230452664109857vboxsync NULL,
cba6719bd64ec749967bbe931230452664109857vboxsync /* u32EndVersion */
cba6719bd64ec749967bbe931230452664109857vboxsync PDM_DRVREG_VERSION
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync};
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync