VirtualBox

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

Last change on this file since 33030 was 33016, checked in by vboxsync, 15 years ago

SCSI: Query parameters only for a read or write

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

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