VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvSCSI.cpp@ 29278

最後變更 在這個檔案從29278是 29250,由 vboxsync 提交於 15 年 前

iprt/asm*.h: split out asm-math.h, don't include asm-*.h from asm.h, don't include asm.h from sup.h. Fixed a couple file headers.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 26.4 KB
 
1/* $Id: DrvSCSI.cpp 29250 2010-05-09 17:53:58Z vboxsync $ */
2/** @file
3 * VBox storage drivers: Generic SCSI command parser and execution driver
4 */
5
6/*
7 * Copyright (C) 2006-2010 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21//#define DEBUG
22#define LOG_GROUP LOG_GROUP_DRV_SCSI
23#include <VBox/pdmdrv.h>
24#include <VBox/pdmifs.h>
25#include <VBox/pdmthread.h>
26#include <VBox/vscsi.h>
27#include <iprt/asm.h>
28#include <iprt/assert.h>
29#include <iprt/mem.h>
30#include <iprt/req.h>
31#include <iprt/semaphore.h>
32#include <iprt/string.h>
33#include <iprt/uuid.h>
34
35#include "Builtins.h"
36
37/** The maximum number of release log entries per device. */
38#define MAX_LOG_REL_ERRORS 1024
39
40/**
41 * SCSI driver instance data.
42 *
43 * @implements PDMISCSICONNECTOR
44 * @implements PDMIBLOCKASYNCPORT
45 * @implements PDMIMOUNTNOTIFY
46 */
47typedef struct DRVSCSI
48{
49 /** Pointer driver instance. */
50 PPDMDRVINS pDrvIns;
51
52 /** Pointer to the attached driver's base interface. */
53 PPDMIBASE pDrvBase;
54 /** Pointer to the attached driver's block interface. */
55 PPDMIBLOCK pDrvBlock;
56 /** Pointer to the attached driver's async block interface. */
57 PPDMIBLOCKASYNC pDrvBlockAsync;
58 /** Pointer to the attached driver's block bios interface. */
59 PPDMIBLOCKBIOS pDrvBlockBios;
60 /** Pointer to the attached driver's mount interface. */
61 PPDMIMOUNT pDrvMount;
62 /** Pointer to the SCSI port interface of the device above. */
63 PPDMISCSIPORT pDevScsiPort;
64 /** pointer to the Led port interface of the dveice above. */
65 PPDMILEDPORTS pLedPort;
66 /** The scsi connector interface .*/
67 PDMISCSICONNECTOR ISCSIConnector;
68 /** The block port interface. */
69 PDMIBLOCKPORT IPort;
70 /** The optional block async port interface. */
71 PDMIBLOCKASYNCPORT IPortAsync;
72#if 0 /* these interfaces aren't implemented */
73 /** The mount notify interface. */
74 PDMIMOUNTNOTIFY IMountNotify;
75#endif
76 /** Fallback status LED state for this drive.
77 * This is used in case the device doesn't has a LED interface. */
78 PDMLED Led;
79 /** Pointer to the status LED for this drive. */
80 PPDMLED pLed;
81
82 /** VSCSI device handle. */
83 VSCSIDEVICE hVScsiDevice;
84 /** VSCSI LUN handle. */
85 VSCSILUN hVScsiLun;
86 /** I/O callbacks. */
87 VSCSILUNIOCALLBACKS VScsiIoCallbacks;
88
89 /** The dedicated I/O thread for the non async approach. */
90 PPDMTHREAD pAsyncIOThread;
91 /** Queue for passing the requests to the thread. */
92 PRTREQQUEUE pQueueRequests;
93 /** Request that we've left pending on wakeup or reset. */
94 PRTREQ pPendingDummyReq;
95 /** Indicates whether PDMDrvHlpAsyncNotificationCompleted should be called by
96 * any of the dummy functions. */
97 bool volatile fDummySignal;
98 /** Release statistics: number of bytes written. */
99 STAMCOUNTER StatBytesWritten;
100 /** Release statistics: number of bytes read. */
101 STAMCOUNTER StatBytesRead;
102 /** Release statistics: Current I/O depth. */
103 volatile uint32_t StatIoDepth;
104 /** Errors printed in the release log. */
105 unsigned cErrors;
106} DRVSCSI, *PDRVSCSI;
107
108/** Converts a pointer to DRVSCSI::ISCSIConnector to a PDRVSCSI. */
109#define PDMISCSICONNECTOR_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, ISCSIConnector)) )
110/** Converts a pointer to DRVSCSI::IPortAsync to a PDRVSCSI. */
111#define PDMIBLOCKASYNCPORT_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, IPortAsync)) )
112
113static int drvscsiProcessRequestOne(PDRVSCSI pThis, VSCSIIOREQ hVScsiIoReq)
114{
115 int rc = VINF_SUCCESS;
116 VSCSIIOREQTXDIR enmTxDir;
117
118 enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
119
120 switch (enmTxDir)
121 {
122 case VSCSIIOREQTXDIR_FLUSH:
123 {
124 rc = pThis->pDrvBlock->pfnFlush(pThis->pDrvBlock);
125 break;
126 }
127 case VSCSIIOREQTXDIR_READ:
128 case VSCSIIOREQTXDIR_WRITE:
129 {
130 uint64_t uOffset = 0;
131 size_t cbTransfer = 0;
132 size_t cbSeg = 0;
133 PCRTSGSEG paSeg = NULL;
134 unsigned cSeg = 0;
135
136 rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer, &cSeg, &cbSeg,
137 &paSeg);
138 AssertRC(rc);
139
140 while (cbTransfer && cSeg)
141 {
142 size_t cbProcess = (cbTransfer < paSeg->cbSeg) ? cbTransfer : paSeg->cbSeg;
143
144 Log(("%s: uOffset=%llu cbProcess=%u\n", __FUNCTION__, uOffset, cbProcess));
145
146 if (enmTxDir == VSCSIIOREQTXDIR_READ)
147 {
148 pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
149 rc = pThis->pDrvBlock->pfnRead(pThis->pDrvBlock, uOffset,
150 paSeg->pvSeg, cbProcess);
151 pThis->pLed->Actual.s.fReading = 0;
152 if (RT_FAILURE(rc))
153 AssertMsgFailed(("%s: Failed to read data %Rrc\n", __FUNCTION__, rc));
154 STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbProcess);
155 }
156 else
157 {
158 pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
159 rc = pThis->pDrvBlock->pfnWrite(pThis->pDrvBlock, uOffset,
160 paSeg->pvSeg, cbProcess);
161 pThis->pLed->Actual.s.fWriting = 0;
162 if (RT_FAILURE(rc))
163 AssertMsgFailed(("%s: Failed to write data %Rrc\n", __FUNCTION__, rc));
164 STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbProcess);
165 }
166
167 /* Go to the next entry. */
168 uOffset += cbProcess;
169 cbTransfer -= cbProcess;
170 paSeg++;
171 cSeg--;
172 }
173
174 break;
175 }
176 default:
177 AssertMsgFailed(("Invalid transfer direction %d\n", enmTxDir));
178 }
179
180 ASMAtomicDecU32(&pThis->StatIoDepth);
181 VSCSIIoReqCompleted(hVScsiIoReq, rc);
182
183 return VINF_SUCCESS;
184}
185
186static int drvscsiGetSize(VSCSILUN hVScsiLun, void *pvScsiLunUser, uint64_t *pcbSize)
187{
188 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
189
190 *pcbSize = pThis->pDrvBlock->pfnGetSize(pThis->pDrvBlock);
191
192 return VINF_SUCCESS;
193}
194
195static int drvscsiTransferCompleteNotify(PPDMIBLOCKASYNCPORT pInterface, void *pvUser, int rc)
196{
197 PDRVSCSI pThis = PDMIBLOCKASYNCPORT_2_DRVSCSI(pInterface);
198 VSCSIIOREQ hVScsiIoReq = (VSCSIIOREQ)pvUser;
199 VSCSIIOREQTXDIR enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
200
201 LogFlowFunc(("Request hVScsiIoReq=%#p completed\n", hVScsiIoReq));
202
203 if (enmTxDir == VSCSIIOREQTXDIR_READ)
204 pThis->pLed->Actual.s.fReading = 0;
205 else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
206 pThis->pLed->Actual.s.fWriting = 0;
207 else
208 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
209
210 ASMAtomicDecU32(&pThis->StatIoDepth);
211 VSCSIIoReqCompleted(hVScsiIoReq, rc);
212
213 return VINF_SUCCESS;
214}
215
216static int drvscsiReqTransferEnqueue(VSCSILUN hVScsiLun,
217 void *pvScsiLunUser,
218 VSCSIIOREQ hVScsiIoReq)
219{
220 int rc = VINF_SUCCESS;
221 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
222
223 ASMAtomicIncU32(&pThis->StatIoDepth);
224
225 if (pThis->pDrvBlockAsync)
226 {
227 /* async I/O path. */
228 VSCSIIOREQTXDIR enmTxDir;
229
230 LogFlowFunc(("Enqueuing hVScsiIoReq=%#p\n", hVScsiIoReq));
231
232 enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
233
234 switch (enmTxDir)
235 {
236 case VSCSIIOREQTXDIR_FLUSH:
237 {
238 rc = pThis->pDrvBlockAsync->pfnStartFlush(pThis->pDrvBlockAsync, hVScsiIoReq);
239 if ( RT_FAILURE(rc)
240 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS
241 && pThis->cErrors++ < MAX_LOG_REL_ERRORS)
242 LogRel(("SCSI#%u: Flush returned rc=%Rrc\n",
243 pThis->pDrvIns->iInstance, rc));
244 break;
245 }
246 case VSCSIIOREQTXDIR_READ:
247 case VSCSIIOREQTXDIR_WRITE:
248 {
249 uint64_t uOffset = 0;
250 size_t cbTransfer = 0;
251 size_t cbSeg = 0;
252 PCRTSGSEG paSeg = NULL;
253 unsigned cSeg = 0;
254
255 rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer,
256 &cSeg, &cbSeg, &paSeg);
257 AssertRC(rc);
258
259 if (enmTxDir == VSCSIIOREQTXDIR_READ)
260 {
261 pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
262 rc = pThis->pDrvBlockAsync->pfnStartRead(pThis->pDrvBlockAsync, uOffset,
263 paSeg, cSeg, cbTransfer,
264 hVScsiIoReq);
265 STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbTransfer);
266 }
267 else
268 {
269 pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
270 rc = pThis->pDrvBlockAsync->pfnStartWrite(pThis->pDrvBlockAsync, uOffset,
271 paSeg, cSeg, cbTransfer,
272 hVScsiIoReq);
273 STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbTransfer);
274 }
275
276 if ( RT_FAILURE(rc)
277 && rc != VERR_VD_ASYNC_IO_IN_PROGRESS
278 && pThis->cErrors++ < MAX_LOG_REL_ERRORS)
279 LogRel(("SCSI#%u: %s at offset %llu (%u bytes left) returned rc=%Rrc\n",
280 pThis->pDrvIns->iInstance,
281 enmTxDir == VSCSIIOREQTXDIR_READ
282 ? "Read"
283 : "Write",
284 uOffset,
285 cbTransfer, rc));
286 break;
287 }
288 default:
289 AssertMsgFailed(("Invalid transfer direction %u\n", enmTxDir));
290 }
291
292 if (rc == VINF_VD_ASYNC_IO_FINISHED)
293 {
294 if (enmTxDir == VSCSIIOREQTXDIR_READ)
295 pThis->pLed->Actual.s.fReading = 0;
296 else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
297 pThis->pLed->Actual.s.fWriting = 0;
298 else
299 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
300
301 ASMAtomicDecU32(&pThis->StatIoDepth);
302 VSCSIIoReqCompleted(hVScsiIoReq, VINF_SUCCESS);
303 }
304 else if (rc == VERR_VD_ASYNC_IO_IN_PROGRESS)
305 rc = VINF_SUCCESS;
306 else if (RT_FAILURE(rc))
307 {
308 if (enmTxDir == VSCSIIOREQTXDIR_READ)
309 pThis->pLed->Actual.s.fReading = 0;
310 else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
311 pThis->pLed->Actual.s.fWriting = 0;
312 else
313 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
314
315 ASMAtomicDecU32(&pThis->StatIoDepth);
316 VSCSIIoReqCompleted(hVScsiIoReq, rc);
317 rc = VINF_SUCCESS;
318 }
319 else
320 AssertMsgFailed(("Invalid return code rc=%Rrc\n", rc));
321 }
322 else
323 {
324 /* I/O thread. */
325 rc = RTReqCallEx(pThis->pQueueRequests, NULL, 0, RTREQFLAGS_NO_WAIT,
326 (PFNRT)drvscsiProcessRequestOne, 2, pThis, hVScsiIoReq);
327 }
328
329 return rc;
330}
331
332static void drvscsiVScsiReqCompleted(VSCSIDEVICE hVScsiDevice, void *pVScsiDeviceUser,
333 void *pVScsiReqUser, int rcReq)
334{
335 PDRVSCSI pThis = (PDRVSCSI)pVScsiDeviceUser;
336
337 pThis->pDevScsiPort->pfnSCSIRequestCompleted(pThis->pDevScsiPort, (PPDMSCSIREQUEST)pVScsiReqUser,
338 rcReq);
339}
340
341/**
342 * Dummy request function used by drvscsiReset to wait for all pending requests
343 * to complete prior to the device reset.
344 *
345 * @param pThis Pointer to the instace data.
346 * @returns VINF_SUCCESS.
347 */
348static int drvscsiAsyncIOLoopSyncCallback(PDRVSCSI pThis)
349{
350 if (pThis->fDummySignal)
351 PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
352 return VINF_SUCCESS;
353}
354
355/**
356 * Request function to wakeup the thread.
357 *
358 * @param pThis Pointer to the instace data.
359 * @returns VWRN_STATE_CHANGED.
360 */
361static int drvscsiAsyncIOLoopWakeupFunc(PDRVSCSI pThis)
362{
363 if (pThis->fDummySignal)
364 PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
365 return VWRN_STATE_CHANGED;
366}
367
368/**
369 * The thread function which processes the requests asynchronously.
370 *
371 * @returns VBox status code.
372 * @param pDrvIns Pointer to the driver instance data.
373 * @param pThread Pointer to the thread instance data.
374 */
375static int drvscsiAsyncIOLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
376{
377 int rc = VINF_SUCCESS;
378 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
379
380 LogFlowFunc(("Entering async IO loop.\n"));
381
382 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
383 return VINF_SUCCESS;
384
385 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
386 {
387 rc = RTReqProcess(pThis->pQueueRequests, RT_INDEFINITE_WAIT);
388 AssertMsg(rc == VWRN_STATE_CHANGED, ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n", rc));
389 }
390
391 return VINF_SUCCESS;
392}
393
394/**
395 * Deals with any pending dummy request
396 *
397 * @returns true if no pending dummy request, false if still pending.
398 * @param pThis The instance data.
399 * @param cMillies The number of milliseconds to wait for any
400 * pending request to finish.
401 */
402static bool drvscsiAsyncIOLoopNoPendingDummy(PDRVSCSI pThis, uint32_t cMillies)
403{
404 if (!pThis->pPendingDummyReq)
405 return true;
406 int rc = RTReqWait(pThis->pPendingDummyReq, cMillies);
407 if (RT_FAILURE(rc))
408 return false;
409 RTReqFree(pThis->pPendingDummyReq);
410 pThis->pPendingDummyReq = NULL;
411 return true;
412}
413
414static int drvscsiAsyncIOLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
415{
416 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
417 PRTREQ pReq;
418 int rc;
419
420 AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
421
422 if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 10000 /* 10 sec */))
423 {
424 LogRel(("drvscsiAsyncIOLoopWakeup#%u: previous dummy request is still pending\n", pDrvIns->iInstance));
425 return VERR_TIMEOUT;
426 }
427
428 rc = RTReqCall(pThis->pQueueRequests, &pReq, 10000 /* 10 sec. */, (PFNRT)drvscsiAsyncIOLoopWakeupFunc, 1, pThis);
429 if (RT_SUCCESS(rc))
430 RTReqFree(pReq);
431 else
432 {
433 pThis->pPendingDummyReq = pReq;
434 LogRel(("drvscsiAsyncIOLoopWakeup#%u: %Rrc pReq=%p\n", pDrvIns->iInstance, rc, pReq));
435 }
436
437 return rc;
438}
439
440/* -=-=-=-=- ISCSIConnector -=-=-=-=- */
441
442/** @copydoc PDMISCSICONNECTOR::pfnSCSIRequestSend. */
443static DECLCALLBACK(int) drvscsiRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
444{
445 int rc;
446 PDRVSCSI pThis = PDMISCSICONNECTOR_2_DRVSCSI(pInterface);
447 VSCSIREQ hVScsiReq;
448
449 rc = VSCSIDeviceReqCreate(pThis->hVScsiDevice, &hVScsiReq,
450 pSCSIRequest->uLogicalUnit,
451 pSCSIRequest->pbCDB,
452 pSCSIRequest->cbCDB,
453 pSCSIRequest->cbScatterGather,
454 pSCSIRequest->cScatterGatherEntries,
455 pSCSIRequest->paScatterGatherHead,
456 pSCSIRequest->pbSenseBuffer,
457 pSCSIRequest->cbSenseBuffer,
458 pSCSIRequest);
459 if (RT_FAILURE(rc))
460 return rc;
461
462 rc = VSCSIDeviceReqEnqueue(pThis->hVScsiDevice, hVScsiReq);
463
464 return rc;
465}
466
467/* -=-=-=-=- IBase -=-=-=-=- */
468
469/**
470 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
471 */
472static DECLCALLBACK(void *) drvscsiQueryInterface(PPDMIBASE pInterface, const char *pszIID)
473{
474 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
475 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
476
477 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
478 PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSICONNECTOR, &pThis->ISCSIConnector);
479 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBLOCKPORT, &pThis->IPort);
480 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBLOCKASYNCPORT, &pThis->IPortAsync);
481 return NULL;
482}
483
484/**
485 * Worker for drvscsiReset, drvscsiSuspend and drvscsiPowerOff.
486 *
487 * @param pDrvIns The driver instance.
488 * @param pfnAsyncNotify The async callback.
489 */
490static void drvscsiR3ResetOrSuspendOrPowerOff(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
491{
492 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
493
494 if (!pThis->pQueueRequests)
495 return;
496
497 ASMAtomicWriteBool(&pThis->fDummySignal, true);
498 if (drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
499 {
500 if (!RTReqIsBusy(pThis->pQueueRequests))
501 {
502 ASMAtomicWriteBool(&pThis->fDummySignal, false);
503 return;
504 }
505
506 PRTREQ pReq;
507 int rc = RTReqCall(pThis->pQueueRequests, &pReq, 0 /*ms*/, (PFNRT)drvscsiAsyncIOLoopSyncCallback, 1, pThis);
508 if (RT_SUCCESS(rc))
509 {
510 ASMAtomicWriteBool(&pThis->fDummySignal, false);
511 RTReqFree(pReq);
512 return;
513 }
514
515 pThis->pPendingDummyReq = pReq;
516 }
517 PDMDrvHlpSetAsyncNotification(pDrvIns, pfnAsyncNotify);
518}
519
520/**
521 * Callback employed by drvscsiSuspend and drvscsiPowerOff.
522 *
523 * @returns true if we've quiesced, false if we're still working.
524 * @param pDrvIns The driver instance.
525 */
526static DECLCALLBACK(bool) drvscsiIsAsyncSuspendOrPowerOffDone(PPDMDRVINS pDrvIns)
527{
528 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
529
530 if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
531 return false;
532 ASMAtomicWriteBool(&pThis->fDummySignal, false);
533 PDMR3ThreadSuspend(pThis->pAsyncIOThread);
534 return true;
535}
536
537/**
538 * @copydoc FNPDMDRVPOWEROFF
539 */
540static DECLCALLBACK(void) drvscsiPowerOff(PPDMDRVINS pDrvIns)
541{
542 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
543}
544
545/**
546 * @copydoc FNPDMDRVSUSPEND
547 */
548static DECLCALLBACK(void) drvscsiSuspend(PPDMDRVINS pDrvIns)
549{
550 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
551}
552
553/**
554 * Callback employed by drvscsiReset.
555 *
556 * @returns true if we've quiesced, false if we're still working.
557 * @param pDrvIns The driver instance.
558 */
559static DECLCALLBACK(bool) drvscsiIsAsyncResetDone(PPDMDRVINS pDrvIns)
560{
561 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
562
563 if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 0 /*ms*/))
564 return false;
565 ASMAtomicWriteBool(&pThis->fDummySignal, false);
566 return true;
567}
568
569/**
570 * @copydoc FNPDMDRVRESET
571 */
572static DECLCALLBACK(void) drvscsiReset(PPDMDRVINS pDrvIns)
573{
574 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncResetDone);
575}
576
577/**
578 * Destruct a driver instance.
579 *
580 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
581 * resources can be freed correctly.
582 *
583 * @param pDrvIns The driver instance data.
584 */
585static DECLCALLBACK(void) drvscsiDestruct(PPDMDRVINS pDrvIns)
586{
587 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
588 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
589
590 if (pThis->pQueueRequests)
591 {
592 if (!drvscsiAsyncIOLoopNoPendingDummy(pThis, 100 /*ms*/))
593 LogRel(("drvscsiDestruct#%u: previous dummy request is still pending\n", pDrvIns->iInstance));
594
595 int rc = RTReqDestroyQueue(pThis->pQueueRequests);
596 AssertMsgRC(rc, ("Failed to destroy queue rc=%Rrc\n", rc));
597 }
598
599 /* Free the VSCSI device and LUN handle. */
600 VSCSILUN hVScsiLun;
601 int rc = VSCSIDeviceLunDetach(pThis->hVScsiDevice, 0, &hVScsiLun);
602 AssertRC(rc);
603
604 Assert(hVScsiLun == pThis->hVScsiLun);
605 rc = VSCSILunDestroy(hVScsiLun);
606 AssertRC(rc);
607 rc = VSCSIDeviceDestroy(pThis->hVScsiDevice);
608 AssertRC(rc);
609}
610
611/**
612 * Construct a block driver instance.
613 *
614 * @copydoc FNPDMDRVCONSTRUCT
615 */
616static DECLCALLBACK(int) drvscsiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
617{
618 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
619 LogFlowFunc(("pDrvIns=%#p pCfg=%#p\n", pDrvIns, pCfg));
620 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
621
622 /*
623 * Initialize the instance data.
624 */
625 pThis->pDrvIns = pDrvIns;
626 pThis->ISCSIConnector.pfnSCSIRequestSend = drvscsiRequestSend;
627
628 pDrvIns->IBase.pfnQueryInterface = drvscsiQueryInterface;
629
630 pThis->IPortAsync.pfnTransferCompleteNotify = drvscsiTransferCompleteNotify;
631
632 /*
633 * Try attach driver below and query it's block interface.
634 */
635 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pThis->pDrvBase);
636 AssertMsgReturn(RT_SUCCESS(rc), ("Attaching driver below failed rc=%Rrc\n", rc), rc);
637
638 /*
639 * Query the block and blockbios interfaces.
640 */
641 pThis->pDrvBlock = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCK);
642 if (!pThis->pDrvBlock)
643 {
644 AssertMsgFailed(("Configuration error: No block interface!\n"));
645 return VERR_PDM_MISSING_INTERFACE;
646 }
647 pThis->pDrvBlockBios = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCKBIOS);
648 if (!pThis->pDrvBlockBios)
649 {
650 AssertMsgFailed(("Configuration error: No block BIOS interface!\n"));
651 return VERR_PDM_MISSING_INTERFACE;
652 }
653
654 /* Query the SCSI port interface above. */
655 pThis->pDevScsiPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISCSIPORT);
656 AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
657
658 pThis->pDrvMount = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMOUNT);
659
660 /* Query the optional LED interface above. */
661 pThis->pLedPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMILEDPORTS);
662 if (pThis->pLedPort != NULL)
663 {
664 /* Get The Led. */
665 rc = pThis->pLedPort->pfnQueryStatusLed(pThis->pLedPort, 0, &pThis->pLed);
666 if (RT_FAILURE(rc))
667 pThis->pLed = &pThis->Led;
668 }
669 else
670 pThis->pLed = &pThis->Led;
671
672 /* Try to get the optional async block interface. */
673 pThis->pDrvBlockAsync = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIBLOCKASYNC);
674
675 PDMBLOCKTYPE enmType = pThis->pDrvBlock->pfnGetType(pThis->pDrvBlock);
676 if (enmType != PDMBLOCKTYPE_HARD_DISK)
677 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_UNSUPPORTED_BLOCK_TYPE, RT_SRC_POS,
678 N_("Only hard disks are currently supported as SCSI devices (enmType=%d)"),
679 enmType);
680
681 /* Create VSCSI device and LUN. */
682 pThis->VScsiIoCallbacks.pfnVScsiLunMediumGetSize = drvscsiGetSize;
683 pThis->VScsiIoCallbacks.pfnVScsiLunReqTransferEnqueue = drvscsiReqTransferEnqueue;
684
685 rc = VSCSIDeviceCreate(&pThis->hVScsiDevice, drvscsiVScsiReqCompleted, pThis);
686 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI device rc=%Rrc\n"), rc);
687 rc = VSCSILunCreate(&pThis->hVScsiLun, VSCSILUNTYPE_SBC, &pThis->VScsiIoCallbacks,
688 pThis);
689 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI LUN rc=%Rrc\n"), rc);
690 rc = VSCSIDeviceLunAttach(pThis->hVScsiDevice, pThis->hVScsiLun, 0);
691 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to attached the LUN to the SCSI device\n"), rc);
692
693 /* Create request queue. */
694 rc = RTReqCreateQueue(&pThis->pQueueRequests);
695 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create request queue rc=%Rrc\n"), rc);
696
697 /* Register statistics counter. */
698 /** @todo aeichner: Find a way to put the instance number of the attached
699 * controller device when we support more than one controller of the same type.
700 * At the moment we have the 0 hardcoded. */
701 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
702 "Amount of data read.", "/Devices/SCSI0/%d/ReadBytes", pDrvIns->iInstance);
703 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
704 "Amount of data written.", "/Devices/SCSI0/%d/WrittenBytes", pDrvIns->iInstance);
705
706 pThis->StatIoDepth = 0;
707
708 PDMDrvHlpSTAMRegisterF(pDrvIns, (void *)&pThis->StatIoDepth, STAMTYPE_U32, STAMVISIBILITY_ALWAYS, STAMUNIT_COUNT,
709 "Number of active tasks.", "/Devices/SCSI0/%d/IoDepth", pDrvIns->iInstance);
710
711
712 /* Create I/O thread. */
713 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pAsyncIOThread, pThis, drvscsiAsyncIOLoop,
714 drvscsiAsyncIOLoopWakeup, 0, RTTHREADTYPE_IO, "SCSI async IO");
715 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create async I/O thread rc=%Rrc\n"), rc);
716
717 return VINF_SUCCESS;
718}
719
720/**
721 * SCSI driver registration record.
722 */
723const PDMDRVREG g_DrvSCSI =
724{
725 /* u32Version */
726 PDM_DRVREG_VERSION,
727 /* szName */
728 "SCSI",
729 /* szRCMod */
730 "",
731 /* szR0Mod */
732 "",
733 /* pszDescription */
734 "Generic SCSI driver.",
735 /* fFlags */
736 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
737 /* fClass. */
738 PDM_DRVREG_CLASS_SCSI,
739 /* cMaxInstances */
740 ~0,
741 /* cbInstance */
742 sizeof(DRVSCSI),
743 /* pfnConstruct */
744 drvscsiConstruct,
745 /* pfnDestruct */
746 drvscsiDestruct,
747 /* pfnRelocate */
748 NULL,
749 /* pfnIOCtl */
750 NULL,
751 /* pfnPowerOn */
752 NULL,
753 /* pfnReset */
754 drvscsiReset,
755 /* pfnSuspend */
756 drvscsiSuspend,
757 /* pfnResume */
758 NULL,
759 /* pfnAttach */
760 NULL,
761 /* pfnDetach */
762 NULL,
763 /* pfnPowerOff */
764 drvscsiPowerOff,
765 /* pfnSoftReset */
766 NULL,
767 /* u32EndVersion */
768 PDM_DRVREG_VERSION
769};
770
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette