VirtualBox

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

Last change on this file was 106241, checked in by vboxsync, 2 months ago

DrvSCSI: When completing SCSI commands, copy only the amount of data provided by device rather than entire buffer size (see bugref:10774).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 57.2 KB
Line 
1/* $Id: DrvSCSI.cpp 106241 2024-10-08 15:20:56Z vboxsync $ */
2/** @file
3 * VBox storage drivers: Generic SCSI command parser and execution driver
4 */
5
6/*
7 * Copyright (C) 2006-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28
29/*********************************************************************************************************************************
30* Header Files *
31*********************************************************************************************************************************/
32//#define DEBUG
33#define LOG_GROUP LOG_GROUP_DRV_SCSI
34#include <VBox/vmm/pdmdrv.h>
35#include <VBox/vmm/pdmifs.h>
36#include <VBox/vmm/pdmqueue.h>
37#include <VBox/vmm/pdmstorageifs.h>
38#include <VBox/vmm/pdmthread.h>
39#include <VBox/vscsi.h>
40#include <VBox/scsi.h>
41#include <iprt/asm.h>
42#include <iprt/assert.h>
43#include <iprt/mem.h>
44#include <iprt/req.h>
45#include <iprt/semaphore.h>
46#include <iprt/string.h>
47#include <iprt/uuid.h>
48
49#include "VBoxDD.h"
50
51/** The maximum number of release log entries per device. */
52#define MAX_LOG_REL_ERRORS 1024
53
54/**
55 * Eject state.
56 */
57typedef struct DRVSCSIEJECTSTATE
58{
59 /** The item core for the PDM queue. */
60 PDMQUEUEITEMCORE Core;
61 /** Event semaphore to signal when complete. */
62 RTSEMEVENT hSemEvt;
63 /** Status of the eject operation. */
64 int rcReq;
65} DRVSCSIEJECTSTATE;
66typedef DRVSCSIEJECTSTATE *PDRVSCSIEJECTSTATE;
67
68/**
69 * SCSI driver private per request data.
70 */
71typedef struct DRVSCSIREQ
72{
73 /** Size of the guest buffer. */
74 size_t cbBuf;
75 /** Temporary buffer holding the data. */
76 void *pvBuf;
77 /** Data segment. */
78 RTSGSEG Seg;
79 /** Transfer direction. */
80 PDMMEDIAEXIOREQSCSITXDIR enmXferDir;
81 /** The VSCSI request handle. */
82 VSCSIREQ hVScsiReq;
83 /** Where to store the SCSI status code. */
84 uint8_t *pu8ScsiSts;
85 /** Where to store the amount of sense data written, optional. */
86 size_t *pcbSense;
87 /** Where to store the transfer direction determined by the VSCSI layer, optional. */
88 PDMMEDIAEXIOREQSCSITXDIR *penmXferDir;
89 /** Transfer size determined by the VSCSI layer. */
90 size_t cbXfer;
91 /** Start of the request data for the device above us. */
92 uint8_t abAlloc[1];
93} DRVSCSIREQ;
94/** Pointer to the driver private per request data. */
95typedef DRVSCSIREQ *PDRVSCSIREQ;
96
97/**
98 * SCSI driver instance data.
99 *
100 * @implements PDMIMEDIAEXPORT
101 * @implements PDMIMEDIAEX
102 * @implements PDMIMOUNTNOTIFY
103 */
104typedef struct DRVSCSI
105{
106 /** Pointer driver instance. */
107 PPDMDRVINS pDrvIns;
108
109 /** Pointer to the attached driver's base interface. */
110 PPDMIBASE pDrvBase;
111 /** Pointer to the attached driver's block interface. */
112 PPDMIMEDIA pDrvMedia;
113 /** Pointer to the attached driver's extended media interface. */
114 PPDMIMEDIAEX pDrvMediaEx;
115 /** Pointer to the attached driver's mount interface. */
116 PPDMIMOUNT pDrvMount;
117 /** Pointer to the extended media port interface of the device above. */
118 PPDMIMEDIAEXPORT pDevMediaExPort;
119 /** Pointer to the media port interface of the device above. */
120 PPDMIMEDIAPORT pDevMediaPort;
121 /** pointer to the Led port interface of the dveice above. */
122 PPDMILEDPORTS pLedPort;
123 /** The media interface for the device above. */
124 PDMIMEDIA IMedia;
125 /** The extended media interface for the device above. */
126 PDMIMEDIAEX IMediaEx;
127 /** The media port interface. */
128 PDMIMEDIAPORT IPort;
129 /** The optional extended media port interface. */
130 PDMIMEDIAEXPORT IPortEx;
131 /** The mount notify interface. */
132 PDMIMOUNTNOTIFY IMountNotify;
133 /** Fallback status LED state for this drive.
134 * This is used in case the device doesn't has a LED interface. */
135 PDMLED Led;
136 /** Pointer to the status LED for this drive. */
137 PPDMLED pLed;
138
139 /** VSCSI device handle. */
140 VSCSIDEVICE hVScsiDevice;
141 /** VSCSI LUN handle. */
142 VSCSILUN hVScsiLun;
143 /** I/O callbacks. */
144 VSCSILUNIOCALLBACKS VScsiIoCallbacks;
145
146 /** Indicates whether PDMDrvHlpAsyncNotificationCompleted should be called by
147 * any of the dummy functions. */
148 bool volatile fDummySignal;
149 /** Current I/O depth. */
150 volatile uint32_t StatIoDepth;
151 /** Errors printed in the release log. */
152 unsigned cErrors;
153
154 /** Size of the I/O request to allocate. */
155 size_t cbIoReqAlloc;
156 /** Size of a VSCSI I/O request. */
157 size_t cbVScsiIoReqAlloc;
158 /** Queue to defer unmounting to EMT. */
159 PDMQUEUEHANDLE hQueue;
160} DRVSCSI, *PDRVSCSI;
161
162/** Convert a VSCSI I/O request handle to the associated PDMIMEDIAEX I/O request. */
163#define DRVSCSI_VSCSIIOREQ_2_PDMMEDIAEXIOREQ(a_hVScsiIoReq) (*(PPDMMEDIAEXIOREQ)((uint8_t *)(a_hVScsiIoReq) - sizeof(PDMMEDIAEXIOREQ)))
164/** Convert a PDMIMEDIAEX I/O additional request memory to a VSCSI I/O request. */
165#define DRVSCSI_PDMMEDIAEXIOREQ_2_VSCSIIOREQ(a_pvIoReqAlloc) ((VSCSIIOREQ)((uint8_t *)(a_pvIoReqAlloc) + sizeof(PDMMEDIAEXIOREQ)))
166
167/**
168 * Returns whether the given status code indicates a non fatal error.
169 *
170 * @returns True if the error can be fixed by the user after the VM was suspended.
171 * False if not and the error should be reported to the guest.
172 * @param rc The status code to check.
173 */
174DECLINLINE(bool) drvscsiIsRedoPossible(int rc)
175{
176 if ( rc == VERR_DISK_FULL
177 || rc == VERR_FILE_TOO_BIG
178 || rc == VERR_BROKEN_PIPE
179 || rc == VERR_NET_CONNECTION_REFUSED
180 || rc == VERR_VD_DEK_MISSING)
181 return true;
182
183 return false;
184}
185
186
187/**
188 * Converts the given VSCSI transfer direction enum to the appropriate PDM extended media interface one.
189 *
190 * @returns The PDM extended media interface transfer direction.
191 * @param enmVScsiXferDir The VSCSI transfer direction.
192 */
193static PDMMEDIAEXIOREQSCSITXDIR drvscsiVScsiXferDir2PdmMediaExDir(VSCSIXFERDIR enmVScsiXferDir)
194{
195 switch (enmVScsiXferDir)
196 {
197 case VSCSIXFERDIR_UNKNOWN: return PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN;
198 case VSCSIXFERDIR_T2I: return PDMMEDIAEXIOREQSCSITXDIR_FROM_DEVICE;
199 case VSCSIXFERDIR_I2T: return PDMMEDIAEXIOREQSCSITXDIR_TO_DEVICE;
200 case VSCSIXFERDIR_NONE: return PDMMEDIAEXIOREQSCSITXDIR_NONE;
201 default: return PDMMEDIAEXIOREQSCSITXDIR_INVALID;
202 }
203
204 /*return PDMMEDIAEXIOREQSCSITXDIR_INVALID;*/
205}
206
207
208/* -=-=-=-=- VScsiIoCallbacks -=-=-=-=- */
209
210/**
211 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunReqAllocSizeSet}
212 */
213static DECLCALLBACK(int) drvscsiReqAllocSizeSet(VSCSILUN hVScsiLun, void *pvScsiLunUser, size_t cbVScsiIoReqAlloc)
214{
215 RT_NOREF(hVScsiLun);
216 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
217
218 /* We need to store the I/O request handle so we can get it when VSCSI queues an I/O request. */
219 int rc = pThis->pDrvMediaEx->pfnIoReqAllocSizeSet(pThis->pDrvMediaEx, cbVScsiIoReqAlloc + sizeof(PDMMEDIAEXIOREQ));
220 if (RT_SUCCESS(rc))
221 pThis->cbVScsiIoReqAlloc = cbVScsiIoReqAlloc + sizeof(PDMMEDIAEXIOREQ);
222
223 return rc;
224}
225
226/**
227 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunReqAlloc}
228 */
229static DECLCALLBACK(int) drvscsiReqAlloc(VSCSILUN hVScsiLun, void *pvScsiLunUser,
230 uint64_t u64Tag, PVSCSIIOREQ phVScsiIoReq)
231{
232 RT_NOREF(hVScsiLun);
233 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
234 PDMMEDIAEXIOREQ hIoReq;
235 void *pvIoReqAlloc;
236 int rc = pThis->pDrvMediaEx->pfnIoReqAlloc(pThis->pDrvMediaEx, &hIoReq, &pvIoReqAlloc, u64Tag,
237 PDMIMEDIAEX_F_SUSPEND_ON_RECOVERABLE_ERR);
238 if (RT_SUCCESS(rc))
239 {
240 PPDMMEDIAEXIOREQ phIoReq = (PPDMMEDIAEXIOREQ)pvIoReqAlloc;
241
242 *phIoReq = hIoReq;
243 *phVScsiIoReq = (VSCSIIOREQ)(phIoReq + 1);
244 }
245
246 return rc;
247}
248
249/**
250 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunReqFree}
251 */
252static DECLCALLBACK(int) drvscsiReqFree(VSCSILUN hVScsiLun, void *pvScsiLunUser, VSCSIIOREQ hVScsiIoReq)
253{
254 RT_NOREF(hVScsiLun);
255 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
256 PDMMEDIAEXIOREQ hIoReq = DRVSCSI_VSCSIIOREQ_2_PDMMEDIAEXIOREQ(hVScsiIoReq);
257
258 return pThis->pDrvMediaEx->pfnIoReqFree(pThis->pDrvMediaEx, hIoReq);
259}
260
261/**
262 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunMediumGetRegionCount}
263 */
264static DECLCALLBACK(uint32_t) drvscsiGetRegionCount(VSCSILUN hVScsiLun, void *pvScsiLunUser)
265{
266 RT_NOREF(hVScsiLun);
267 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
268
269 return pThis->pDrvMedia->pfnGetRegionCount(pThis->pDrvMedia);
270}
271
272/** @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunMediumQueryRegionProperties} */
273static DECLCALLBACK(int) drvscsiQueryRegionProperties(VSCSILUN hVScsiLun, void *pvScsiLunUser,
274 uint32_t uRegion, uint64_t *pu64LbaStart,
275 uint64_t *pcBlocks, uint64_t *pcbBlock,
276 PVDREGIONDATAFORM penmDataForm)
277{
278 RT_NOREF(hVScsiLun);
279 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
280
281 return pThis->pDrvMedia->pfnQueryRegionProperties(pThis->pDrvMedia, uRegion, pu64LbaStart,
282 pcBlocks, pcbBlock, penmDataForm);
283}
284
285/** @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunMediumQueryRegionPropertiesForLba} */
286static DECLCALLBACK(int) drvscsiQueryRegionPropertiesForLba(VSCSILUN hVScsiLun, void *pvScsiLunUser,
287 uint64_t u64LbaStart, uint32_t *puRegion,
288 uint64_t *pcBlocks, uint64_t *pcbBlock,
289 PVDREGIONDATAFORM penmDataForm)
290{
291 RT_NOREF(hVScsiLun);
292 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
293
294 return pThis->pDrvMedia->pfnQueryRegionPropertiesForLba(pThis->pDrvMedia, u64LbaStart, puRegion,
295 pcBlocks, pcbBlock, penmDataForm);
296}
297
298/**
299 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunMediumSetLock}
300 */
301static DECLCALLBACK(int) drvscsiSetLock(VSCSILUN hVScsiLun, void *pvScsiLunUser, bool fLocked)
302{
303 RT_NOREF(hVScsiLun);
304 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
305
306 if (fLocked)
307 pThis->pDrvMount->pfnLock(pThis->pDrvMount);
308 else
309 pThis->pDrvMount->pfnUnlock(pThis->pDrvMount);
310
311 return VINF_SUCCESS;
312}
313
314/** @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunMediumEject} */
315static DECLCALLBACK(int) drvscsiEject(VSCSILUN hVScsiLun, void *pvScsiLunUser)
316{
317 RT_NOREF(hVScsiLun);
318 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
319 int rc = VINF_SUCCESS;
320 RTSEMEVENT hSemEvt = NIL_RTSEMEVENT;
321
322 /* This must be done from EMT. */
323 rc = RTSemEventCreate(&hSemEvt);
324 if (RT_SUCCESS(rc))
325 {
326 PDRVSCSIEJECTSTATE pEjectState = (PDRVSCSIEJECTSTATE)PDMDrvHlpQueueAlloc(pThis->pDrvIns, pThis->hQueue);
327 if (pEjectState)
328 {
329 pEjectState->hSemEvt = hSemEvt;
330 PDMDrvHlpQueueInsert(pThis->pDrvIns, pThis->hQueue, &pEjectState->Core);
331
332 /* Wait for completion. */
333 rc = RTSemEventWait(pEjectState->hSemEvt, RT_INDEFINITE_WAIT);
334 if (RT_SUCCESS(rc))
335 rc = pEjectState->rcReq;
336 }
337 else
338 rc = VERR_NO_MEMORY;
339
340 RTSemEventDestroy(pEjectState->hSemEvt);
341 }
342
343 return rc;
344}
345
346/**
347 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunReqTransferEnqueue}
348 */
349static DECLCALLBACK(int) drvscsiReqTransferEnqueue(VSCSILUN hVScsiLun, void *pvScsiLunUser, VSCSIIOREQ hVScsiIoReq)
350{
351 RT_NOREF(hVScsiLun);
352 int rc = VINF_SUCCESS;
353 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
354 PDMMEDIAEXIOREQ hIoReq = DRVSCSI_VSCSIIOREQ_2_PDMMEDIAEXIOREQ(hVScsiIoReq);
355
356 LogFlowFunc(("Enqueuing hVScsiIoReq=%#p\n", hVScsiIoReq));
357
358 VSCSIIOREQTXDIR enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
359 switch (enmTxDir)
360 {
361 case VSCSIIOREQTXDIR_FLUSH:
362 {
363 rc = pThis->pDrvMediaEx->pfnIoReqFlush(pThis->pDrvMediaEx, hIoReq);
364 if ( RT_FAILURE(rc)
365 && pThis->cErrors++ < MAX_LOG_REL_ERRORS)
366 LogRel(("SCSI#%u: Flush returned rc=%Rrc\n",
367 pThis->pDrvIns->iInstance, rc));
368 break;
369 }
370 case VSCSIIOREQTXDIR_UNMAP:
371 {
372 PCRTRANGE paRanges;
373 unsigned cRanges;
374
375 rc = VSCSIIoReqUnmapParamsGet(hVScsiIoReq, &paRanges, &cRanges);
376 AssertRC(rc);
377
378 pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
379 rc = pThis->pDrvMediaEx->pfnIoReqDiscard(pThis->pDrvMediaEx, hIoReq, cRanges);
380 if ( RT_FAILURE(rc)
381 && pThis->cErrors++ < MAX_LOG_REL_ERRORS)
382 LogRel(("SCSI#%u: Discard returned rc=%Rrc\n",
383 pThis->pDrvIns->iInstance, rc));
384 break;
385 }
386 case VSCSIIOREQTXDIR_READ:
387 case VSCSIIOREQTXDIR_WRITE:
388 {
389 uint64_t uOffset = 0;
390 size_t cbTransfer = 0;
391 size_t cbSeg = 0;
392 PCRTSGSEG paSeg = NULL;
393 unsigned cSeg = 0;
394
395 rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer,
396 &cSeg, &cbSeg, &paSeg);
397 AssertRC(rc);
398
399 if (enmTxDir == VSCSIIOREQTXDIR_READ)
400 {
401 pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
402 rc = pThis->pDrvMediaEx->pfnIoReqRead(pThis->pDrvMediaEx, hIoReq, uOffset, cbTransfer);
403 }
404 else
405 {
406 pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
407 rc = pThis->pDrvMediaEx->pfnIoReqWrite(pThis->pDrvMediaEx, hIoReq, uOffset, cbTransfer);
408 }
409
410 if ( RT_FAILURE(rc)
411 && pThis->cErrors++ < MAX_LOG_REL_ERRORS)
412 LogRel(("SCSI#%u: %s at offset %llu (%u bytes left) returned rc=%Rrc\n",
413 pThis->pDrvIns->iInstance,
414 enmTxDir == VSCSIIOREQTXDIR_READ
415 ? "Read"
416 : "Write",
417 uOffset,
418 cbTransfer, rc));
419 break;
420 }
421 default:
422 AssertMsgFailed(("Invalid transfer direction %u\n", enmTxDir));
423 }
424
425 if (rc == VINF_SUCCESS)
426 {
427 if (enmTxDir == VSCSIIOREQTXDIR_READ)
428 pThis->pLed->Actual.s.fReading = 0;
429 else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
430 pThis->pLed->Actual.s.fWriting = 0;
431 else
432 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
433
434 VSCSIIoReqCompleted(hVScsiIoReq, VINF_SUCCESS, false);
435 rc = VINF_SUCCESS;
436 }
437 else if (rc == VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
438 rc = VINF_SUCCESS;
439 else if (RT_FAILURE(rc))
440 {
441 if (enmTxDir == VSCSIIOREQTXDIR_READ)
442 pThis->pLed->Actual.s.fReading = 0;
443 else if (enmTxDir == VSCSIIOREQTXDIR_WRITE)
444 pThis->pLed->Actual.s.fWriting = 0;
445 else
446 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
447
448 VSCSIIoReqCompleted(hVScsiIoReq, rc, drvscsiIsRedoPossible(rc));
449 rc = VINF_SUCCESS;
450 }
451 else
452 AssertMsgFailed(("Invalid return code rc=%Rrc\n", rc));
453
454 return rc;
455}
456
457/**
458 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunGetFeatureFlags}
459 */
460static DECLCALLBACK(int) drvscsiGetFeatureFlags(VSCSILUN hVScsiLun, void *pvScsiLunUser, uint64_t *pfFeatures)
461{
462 RT_NOREF(hVScsiLun);
463 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
464
465 *pfFeatures = 0;
466
467 uint32_t fFeatures = 0;
468 int rc = pThis->pDrvMediaEx->pfnQueryFeatures(pThis->pDrvMediaEx, &fFeatures);
469 if (RT_SUCCESS(rc) && (fFeatures & PDMIMEDIAEX_FEATURE_F_DISCARD))
470 *pfFeatures |= VSCSI_LUN_FEATURE_UNMAP;
471
472 if ( pThis->pDrvMedia
473 && pThis->pDrvMedia->pfnIsNonRotational(pThis->pDrvMedia))
474 *pfFeatures |= VSCSI_LUN_FEATURE_NON_ROTATIONAL;
475
476 if (pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia))
477 *pfFeatures |= VSCSI_LUN_FEATURE_READONLY;
478
479 return VINF_SUCCESS;
480}
481
482/**
483 * @interface_method_impl{VSCSILUNIOCALLBACKS,pfnVScsiLunQueryInqStrings}
484 */
485static DECLCALLBACK(int) drvscsiQueryInqStrings(VSCSILUN hVScsiLun, void *pvScsiLunUser, const char **ppszVendorId,
486 const char **ppszProductId, const char **ppszProductLevel)
487{
488 RT_NOREF(hVScsiLun);
489 PDRVSCSI pThis = (PDRVSCSI)pvScsiLunUser;
490
491 if (pThis->pDevMediaPort->pfnQueryScsiInqStrings)
492 return pThis->pDevMediaPort->pfnQueryScsiInqStrings(pThis->pDevMediaPort, ppszVendorId,
493 ppszProductId, ppszProductLevel);
494
495 return VERR_NOT_FOUND;
496}
497
498/* -=-=-=-=- IPortEx -=-=-=-=- */
499
500/**
501 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
502 */
503static DECLCALLBACK(int) drvscsiIoReqCompleteNotify(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
504 void *pvIoReqAlloc, int rcReq)
505{
506 RT_NOREF1(hIoReq);
507
508 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IPortEx);
509 VSCSIIOREQ hVScsiIoReq = DRVSCSI_PDMMEDIAEXIOREQ_2_VSCSIIOREQ(pvIoReqAlloc);
510 VSCSIIOREQTXDIR enmTxDir = VSCSIIoReqTxDirGet(hVScsiIoReq);
511
512 LogFlowFunc(("Request hVScsiIoReq=%#p completed\n", hVScsiIoReq));
513
514 if (enmTxDir == VSCSIIOREQTXDIR_READ)
515 pThis->pLed->Actual.s.fReading = 0;
516 else if ( enmTxDir == VSCSIIOREQTXDIR_WRITE
517 || enmTxDir == VSCSIIOREQTXDIR_UNMAP)
518 pThis->pLed->Actual.s.fWriting = 0;
519 else
520 AssertMsg(enmTxDir == VSCSIIOREQTXDIR_FLUSH, ("Invalid transfer direction %u\n", enmTxDir));
521
522 if (RT_SUCCESS(rcReq))
523 VSCSIIoReqCompleted(hVScsiIoReq, rcReq, false /* fRedoPossible */);
524 else
525 {
526 pThis->cErrors++;
527 if (pThis->cErrors < MAX_LOG_REL_ERRORS)
528 {
529 if (enmTxDir == VSCSIIOREQTXDIR_FLUSH)
530 LogRel(("SCSI#%u: Flush returned rc=%Rrc\n",
531 pThis->pDrvIns->iInstance, rcReq));
532 else if (enmTxDir == VSCSIIOREQTXDIR_UNMAP)
533 LogRel(("SCSI#%u: Unmap returned rc=%Rrc\n",
534 pThis->pDrvIns->iInstance, rcReq));
535 else
536 {
537 uint64_t uOffset = 0;
538 size_t cbTransfer = 0;
539 size_t cbSeg = 0;
540 PCRTSGSEG paSeg = NULL;
541 unsigned cSeg = 0;
542
543 VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer,
544 &cSeg, &cbSeg, &paSeg);
545
546 LogRel(("SCSI#%u: %s at offset %llu (%u bytes left) returned rc=%Rrc\n",
547 pThis->pDrvIns->iInstance,
548 enmTxDir == VSCSIIOREQTXDIR_READ
549 ? "Read"
550 : "Write",
551 uOffset,
552 cbTransfer, rcReq));
553 }
554 }
555
556 VSCSIIoReqCompleted(hVScsiIoReq, rcReq, drvscsiIsRedoPossible(rcReq));
557 }
558
559 return VINF_SUCCESS;
560}
561
562/**
563 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
564 */
565static DECLCALLBACK(int) drvscsiIoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
566 void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf,
567 size_t cbCopy)
568{
569 RT_NOREF2(pInterface, hIoReq);
570
571 VSCSIIOREQ hVScsiIoReq = DRVSCSI_PDMMEDIAEXIOREQ_2_VSCSIIOREQ(pvIoReqAlloc);
572 uint64_t uOffset = 0;
573 size_t cbTransfer = 0;
574 size_t cbSeg = 0;
575 PCRTSGSEG paSeg = NULL;
576 unsigned cSeg = 0;
577 size_t cbCopied = 0;
578
579 int rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer, &cSeg, &cbSeg, &paSeg);
580 if (RT_SUCCESS(rc))
581 {
582 RTSGBUF SgBuf;
583 RTSgBufInit(&SgBuf, paSeg, cSeg);
584
585 RTSgBufAdvance(&SgBuf, offDst);
586 cbCopied = RTSgBufCopy(&SgBuf, pSgBuf, cbCopy);
587 }
588
589 return cbCopied == cbCopy ? VINF_SUCCESS : VERR_PDM_MEDIAEX_IOBUF_OVERFLOW;
590}
591
592/**
593 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
594 */
595static DECLCALLBACK(int) drvscsiIoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
596 void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf,
597 size_t cbCopy)
598{
599 RT_NOREF2(pInterface, hIoReq);
600
601 VSCSIIOREQ hVScsiIoReq = DRVSCSI_PDMMEDIAEXIOREQ_2_VSCSIIOREQ(pvIoReqAlloc);
602 uint64_t uOffset = 0;
603 size_t cbTransfer = 0;
604 size_t cbSeg = 0;
605 PCRTSGSEG paSeg = NULL;
606 unsigned cSeg = 0;
607 size_t cbCopied = 0;
608
609 int rc = VSCSIIoReqParamsGet(hVScsiIoReq, &uOffset, &cbTransfer, &cSeg, &cbSeg, &paSeg);
610 if (RT_SUCCESS(rc))
611 {
612 RTSGBUF SgBuf;
613 RTSgBufInit(&SgBuf, paSeg, cSeg);
614
615 RTSgBufAdvance(&SgBuf, offSrc);
616 cbCopied = RTSgBufCopy(pSgBuf, &SgBuf, cbCopy);
617 }
618
619 return cbCopied == cbCopy ? VINF_SUCCESS : VERR_PDM_MEDIAEX_IOBUF_UNDERRUN;
620}
621
622/**
623 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqQueryDiscardRanges}
624 */
625static DECLCALLBACK(int) drvscsiIoReqQueryDiscardRanges(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
626 void *pvIoReqAlloc, uint32_t idxRangeStart,
627 uint32_t cRanges, PRTRANGE paRanges,
628 uint32_t *pcRanges)
629{
630 RT_NOREF2(pInterface, hIoReq);
631
632 VSCSIIOREQ hVScsiIoReq = DRVSCSI_PDMMEDIAEXIOREQ_2_VSCSIIOREQ(pvIoReqAlloc);
633 PCRTRANGE paRangesVScsi;
634 unsigned cRangesVScsi;
635
636 int rc = VSCSIIoReqUnmapParamsGet(hVScsiIoReq, &paRangesVScsi, &cRangesVScsi);
637 if (RT_SUCCESS(rc))
638 {
639 uint32_t cRangesCopy = RT_MIN(cRangesVScsi - idxRangeStart, cRanges);
640 Assert( idxRangeStart < cRangesVScsi
641 && (idxRangeStart + cRanges) <= cRangesVScsi);
642
643 memcpy(paRanges, &paRangesVScsi[idxRangeStart], cRangesCopy * sizeof(RTRANGE));
644 *pcRanges = cRangesCopy;
645 }
646 return rc;
647}
648
649/**
650 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
651 */
652static DECLCALLBACK(void) drvscsiIoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
653 void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
654{
655 RT_NOREF2(hIoReq, pvIoReqAlloc);
656 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IPortEx);
657
658 switch (enmState)
659 {
660 case PDMMEDIAEXIOREQSTATE_SUSPENDED:
661 {
662 /* Make sure the request is not accounted for so the VM can suspend successfully. */
663 uint32_t cTasksActive = ASMAtomicDecU32(&pThis->StatIoDepth);
664 if (!cTasksActive && pThis->fDummySignal)
665 PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
666 break;
667 }
668 case PDMMEDIAEXIOREQSTATE_ACTIVE:
669 /* Make sure the request is accounted for so the VM suspends only when the request is complete. */
670 ASMAtomicIncU32(&pThis->StatIoDepth);
671 break;
672 default:
673 AssertMsgFailed(("Invalid request state given %u\n", enmState));
674 }
675
676 pThis->pDevMediaExPort->pfnIoReqStateChanged(pThis->pDevMediaExPort, hIoReq, pvIoReqAlloc, enmState);
677}
678
679
680/* -=-=-=-=- IMedia -=-=-=-=- */
681
682/** @interface_method_impl{PDMIMEDIA,pfnGetSize} */
683static DECLCALLBACK(uint64_t) drvscsiGetSize(PPDMIMEDIA pInterface)
684{
685 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
686 return pThis->pDrvMedia->pfnGetSize(pThis->pDrvMedia);
687}
688
689/** @interface_method_impl{PDMIMEDIA,pfnGetSectorSize} */
690static DECLCALLBACK(uint32_t) drvscsiGetSectorSize(PPDMIMEDIA pInterface)
691{
692 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
693 return pThis->pDrvMedia->pfnGetSectorSize(pThis->pDrvMedia);
694}
695
696/** @interface_method_impl{PDMIMEDIA,pfnIsReadOnly} */
697static DECLCALLBACK(bool) drvscsiIsReadOnly(PPDMIMEDIA pInterface)
698{
699 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
700 return pThis->pDrvMedia->pfnIsReadOnly(pThis->pDrvMedia);
701}
702
703/** @interface_method_impl{PDMIMEDIA,pfnIsNonRotational} */
704static DECLCALLBACK(bool) drvscsiIsNonRotational(PPDMIMEDIA pInterface)
705{
706 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
707 return pThis->pDrvMedia->pfnIsNonRotational(pThis->pDrvMedia);
708}
709
710/** @interface_method_impl{PDMIMEDIA,pfnBiosGetPCHSGeometry} */
711static DECLCALLBACK(int) drvscsiBiosGetPCHSGeometry(PPDMIMEDIA pInterface,
712 PPDMMEDIAGEOMETRY pPCHSGeometry)
713{
714 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
715 return pThis->pDrvMedia->pfnBiosGetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
716}
717
718/** @interface_method_impl{PDMIMEDIA,pfnBiosSetPCHSGeometry} */
719static DECLCALLBACK(int) drvscsiBiosSetPCHSGeometry(PPDMIMEDIA pInterface,
720 PCPDMMEDIAGEOMETRY pPCHSGeometry)
721{
722 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
723 return pThis->pDrvMedia->pfnBiosSetPCHSGeometry(pThis->pDrvMedia, pPCHSGeometry);
724}
725
726/** @interface_method_impl{PDMIMEDIA,pfnBiosGetLCHSGeometry} */
727static DECLCALLBACK(int) drvscsiBiosGetLCHSGeometry(PPDMIMEDIA pInterface,
728 PPDMMEDIAGEOMETRY pLCHSGeometry)
729{
730 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
731 return pThis->pDrvMedia->pfnBiosGetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
732}
733
734/** @interface_method_impl{PDMIMEDIA,pfnBiosSetLCHSGeometry} */
735static DECLCALLBACK(int) drvscsiBiosSetLCHSGeometry(PPDMIMEDIA pInterface,
736 PCPDMMEDIAGEOMETRY pLCHSGeometry)
737{
738 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
739 return pThis->pDrvMedia->pfnBiosSetLCHSGeometry(pThis->pDrvMedia, pLCHSGeometry);
740}
741
742/** @interface_method_impl{PDMIMEDIA,pfnBiosIsVisible} */
743static DECLCALLBACK(bool) drvscsiBiosIsVisible(PPDMIMEDIA pInterface)
744{
745 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
746 return pThis->pDrvMedia->pfnBiosIsVisible(pThis->pDrvMedia);
747}
748
749/** @interface_method_impl{PDMIMEDIA,pfnGetType} */
750static DECLCALLBACK(PDMMEDIATYPE) drvscsiGetType(PPDMIMEDIA pInterface)
751{
752 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
753 VSCSILUNTYPE enmLunType;
754 PDMMEDIATYPE enmMediaType = PDMMEDIATYPE_ERROR;
755
756 int rc = VSCSIDeviceLunQueryType(pThis->hVScsiDevice, 0, &enmLunType);
757 if (RT_SUCCESS(rc))
758 {
759 switch (enmLunType)
760 {
761 case VSCSILUNTYPE_SBC:
762 enmMediaType = PDMMEDIATYPE_HARD_DISK;
763 break;
764 case VSCSILUNTYPE_MMC:
765 enmMediaType = PDMMEDIATYPE_CDROM;
766 break;
767 default:
768 enmMediaType = PDMMEDIATYPE_ERROR;
769 break;
770 }
771 }
772
773 return enmMediaType;
774}
775
776/** @interface_method_impl{PDMIMEDIA,pfnGetUuid} */
777static DECLCALLBACK(int) drvscsiGetUuid(PPDMIMEDIA pInterface, PRTUUID pUuid)
778{
779 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMedia);
780
781 int rc = VINF_SUCCESS;
782 if (pThis->pDrvMedia)
783 rc = pThis->pDrvMedia->pfnGetUuid(pThis->pDrvMedia, pUuid);
784 else
785 RTUuidClear(pUuid);
786
787 return rc;
788}
789
790/* -=-=-=-=- IMediaEx -=-=-=-=- */
791
792/** @interface_method_impl{PDMIMEDIAEX,pfnQueryFeatures} */
793static DECLCALLBACK(int) drvscsiQueryFeatures(PPDMIMEDIAEX pInterface, uint32_t *pfFeatures)
794{
795 RT_NOREF1(pInterface);
796
797 *pfFeatures = PDMIMEDIAEX_FEATURE_F_RAWSCSICMD;
798 return VINF_SUCCESS;
799}
800
801/** @interface_method_impl{PDMIMEDIAEX,pfnNotifySuspend} */
802static DECLCALLBACK(void) drvscsiNotifySuspend(PPDMIMEDIAEX pInterface)
803{
804 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMediaEx);
805
806 /** @todo Don't crash if someone screws this up... Recreated a VISO while it
807 * was mounted and asked the GUI to use it. Got forced umount question.
808 * Said yes. Ended up here with a NULL pointer. */
809 PPDMIMEDIAEX pDrvMediaEx = pThis->pDrvMediaEx;
810 if (pDrvMediaEx)
811 pDrvMediaEx->pfnNotifySuspend(pDrvMediaEx);
812}
813
814/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAllocSizeSet} */
815static DECLCALLBACK(int) drvscsiIoReqAllocSizeSet(PPDMIMEDIAEX pInterface, size_t cbIoReqAlloc)
816{
817 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMediaEx);
818
819 pThis->cbIoReqAlloc = RT_UOFFSETOF_DYN(DRVSCSIREQ, abAlloc[cbIoReqAlloc]);
820 return VINF_SUCCESS;
821}
822
823/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqAlloc} */
824static DECLCALLBACK(int) drvscsiIoReqAlloc(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc,
825 PDMMEDIAEXIOREQID uIoReqId, uint32_t fFlags)
826{
827 RT_NOREF2(uIoReqId, fFlags);
828
829 int rc = VINF_SUCCESS;
830 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMediaEx);
831 PDRVSCSIREQ pReq = (PDRVSCSIREQ)RTMemAllocZ(pThis->cbIoReqAlloc);
832 if (RT_LIKELY(pReq))
833 {
834 *phIoReq = (PDMMEDIAEXIOREQ)pReq;
835 *ppvIoReqAlloc = &pReq->abAlloc[0];
836 }
837 else
838 rc = VERR_NO_MEMORY;
839
840 return rc;
841}
842
843/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFree} */
844static DECLCALLBACK(int) drvscsiIoReqFree(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
845{
846 RT_NOREF1(pInterface);
847 PDRVSCSIREQ pReq = (PDRVSCSIREQ)hIoReq;
848
849 RTMemFree(pReq);
850 return VINF_SUCCESS;
851}
852
853/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryResidual} */
854static DECLCALLBACK(int) drvscsiIoReqQueryResidual(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbResidual)
855{
856 RT_NOREF1(pInterface);
857 PDRVSCSIREQ pReq = (PDRVSCSIREQ)hIoReq;
858
859 if (pReq->cbXfer && pReq->cbXfer <= pReq->cbBuf)
860 *pcbResidual = pReq->cbBuf - pReq->cbXfer;
861 else
862 *pcbResidual = 0; /* Overflow/Underrun error or no data transfers. */
863 return VINF_SUCCESS;
864}
865
866/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQueryXferSize} */
867static DECLCALLBACK(int) drvscsiIoReqQueryXferSize(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, size_t *pcbXfer)
868{
869 RT_NOREF1(pInterface);
870 PDRVSCSIREQ pReq = (PDRVSCSIREQ)hIoReq;
871
872 *pcbXfer = pReq->cbXfer;
873 return VINF_SUCCESS;
874}
875
876/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancelAll} */
877static DECLCALLBACK(int) drvscsiIoReqCancelAll(PPDMIMEDIAEX pInterface)
878{
879 RT_NOREF1(pInterface);
880 return VINF_SUCCESS;
881}
882
883/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqCancel} */
884static DECLCALLBACK(int) drvscsiIoReqCancel(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQID uIoReqId)
885{
886 RT_NOREF2(pInterface, uIoReqId);
887 return VERR_PDM_MEDIAEX_IOREQID_NOT_FOUND;
888}
889
890/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqRead} */
891static DECLCALLBACK(int) drvscsiIoReqRead(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbRead)
892{
893 RT_NOREF4(pInterface, hIoReq, off, cbRead);
894 return VERR_NOT_SUPPORTED;
895}
896
897/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqWrite} */
898static DECLCALLBACK(int) drvscsiIoReqWrite(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, uint64_t off, size_t cbWrite)
899{
900 RT_NOREF4(pInterface, hIoReq, off, cbWrite);
901 return VERR_NOT_SUPPORTED;
902}
903
904/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqFlush} */
905static DECLCALLBACK(int) drvscsiIoReqFlush(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq)
906{
907 RT_NOREF2(pInterface, hIoReq);
908 return VERR_NOT_SUPPORTED;
909}
910
911/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqDiscard} */
912static DECLCALLBACK(int) drvscsiIoReqDiscard(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq, unsigned cRangesMax)
913{
914 RT_NOREF3(pInterface, hIoReq, cRangesMax);
915 return VERR_NOT_SUPPORTED;
916}
917
918/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSendScsiCmd} */
919static DECLCALLBACK(int) drvscsiIoReqSendScsiCmd(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
920 uint32_t uLun, const uint8_t *pbCdb, size_t cbCdb,
921 PDMMEDIAEXIOREQSCSITXDIR enmTxDir, PDMMEDIAEXIOREQSCSITXDIR *penmTxDirRet,
922 size_t cbBuf, uint8_t *pabSense, size_t cbSense, size_t *pcbSenseRet,
923 uint8_t *pu8ScsiSts, uint32_t cTimeoutMillies)
924{
925 RT_NOREF1(cTimeoutMillies);
926
927 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMediaEx);
928 PDRVSCSIREQ pReq = (PDRVSCSIREQ)hIoReq;
929 int rc = VINF_SUCCESS;
930
931 Log(("Dump for pReq=%#p Command: %s\n", pReq, SCSICmdText(pbCdb[0])));
932 Log(("cbCdb=%u\n", cbCdb));
933 for (uint32_t i = 0; i < cbCdb; i++)
934 Log(("pbCdb[%u]=%#x\n", i, pbCdb[i]));
935 Log(("cbBuf=%zu\n", cbBuf));
936
937 pReq->enmXferDir = enmTxDir;
938 pReq->cbBuf = cbBuf;
939 pReq->pu8ScsiSts = pu8ScsiSts;
940 pReq->pcbSense = pcbSenseRet;
941 pReq->penmXferDir = penmTxDirRet;
942
943 /* Allocate and sync buffers if a data transfer is indicated. */
944 if (cbBuf)
945 {
946 pReq->pvBuf = RTMemAlloc(cbBuf);
947 if (RT_UNLIKELY(!pReq->pvBuf))
948 rc = VERR_NO_MEMORY;
949 }
950
951 if (RT_SUCCESS(rc))
952 {
953 pReq->Seg.pvSeg = pReq->pvBuf;
954 pReq->Seg.cbSeg = cbBuf;
955
956 if ( cbBuf
957 && ( enmTxDir == PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN
958 || enmTxDir == PDMMEDIAEXIOREQSCSITXDIR_TO_DEVICE))
959 {
960 RTSGBUF SgBuf;
961 RTSgBufInit(&SgBuf, &pReq->Seg, 1);
962 rc = pThis->pDevMediaExPort->pfnIoReqCopyToBuf(pThis->pDevMediaExPort, hIoReq, &pReq->abAlloc[0],
963 0, &SgBuf, cbBuf);
964 }
965
966 if (RT_SUCCESS(rc))
967 {
968 rc = VSCSIDeviceReqCreate(pThis->hVScsiDevice, &pReq->hVScsiReq,
969 uLun, (uint8_t *)pbCdb, cbCdb, cbBuf, 1, &pReq->Seg,
970 pabSense, cbSense, pReq);
971 if (RT_SUCCESS(rc))
972 {
973 ASMAtomicIncU32(&pThis->StatIoDepth);
974 rc = VSCSIDeviceReqEnqueue(pThis->hVScsiDevice, pReq->hVScsiReq);
975 if (RT_SUCCESS(rc))
976 rc = VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS;
977 }
978 }
979 }
980
981 return rc;
982}
983
984/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetActiveCount} */
985static DECLCALLBACK(uint32_t) drvscsiIoReqGetActiveCount(PPDMIMEDIAEX pInterface)
986{
987 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMediaEx);
988 return pThis->StatIoDepth;
989}
990
991/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqGetSuspendedCount} */
992static DECLCALLBACK(uint32_t) drvscsiIoReqGetSuspendedCount(PPDMIMEDIAEX pInterface)
993{
994 RT_NOREF1(pInterface);
995 return 0;
996}
997
998/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedStart} */
999static DECLCALLBACK(int) drvscsiIoReqQuerySuspendedStart(PPDMIMEDIAEX pInterface, PPDMMEDIAEXIOREQ phIoReq, void **ppvIoReqAlloc)
1000{
1001 RT_NOREF3(pInterface, phIoReq, ppvIoReqAlloc);
1002 return VERR_NOT_IMPLEMENTED;
1003}
1004
1005/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqQuerySuspendedNext} */
1006static DECLCALLBACK(int) drvscsiIoReqQuerySuspendedNext(PPDMIMEDIAEX pInterface, PDMMEDIAEXIOREQ hIoReq,
1007 PPDMMEDIAEXIOREQ phIoReqNext, void **ppvIoReqAllocNext)
1008{
1009 RT_NOREF4(pInterface, hIoReq, phIoReqNext, ppvIoReqAllocNext);
1010 return VERR_NOT_IMPLEMENTED;
1011}
1012
1013/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedSave} */
1014static DECLCALLBACK(int) drvscsiIoReqSuspendedSave(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1015{
1016 RT_NOREF3(pInterface, pSSM, hIoReq);
1017 return VERR_NOT_IMPLEMENTED;
1018}
1019
1020/** @interface_method_impl{PDMIMEDIAEX,pfnIoReqSuspendedLoad} */
1021static DECLCALLBACK(int) drvscsiIoReqSuspendedLoad(PPDMIMEDIAEX pInterface, PSSMHANDLE pSSM, PDMMEDIAEXIOREQ hIoReq)
1022{
1023 RT_NOREF3(pInterface, pSSM, hIoReq);
1024 return VERR_NOT_IMPLEMENTED;
1025}
1026
1027
1028static DECLCALLBACK(void) drvscsiIoReqVScsiReqCompleted(VSCSIDEVICE hVScsiDevice, void *pVScsiDeviceUser,
1029 void *pVScsiReqUser, int rcScsiCode, bool fRedoPossible,
1030 int rcReq, size_t cbXfer, VSCSIXFERDIR enmXferDir, size_t cbSense)
1031{
1032 RT_NOREF2(hVScsiDevice, fRedoPossible);
1033 PDRVSCSI pThis = (PDRVSCSI)pVScsiDeviceUser;
1034 PDRVSCSIREQ pReq = (PDRVSCSIREQ)pVScsiReqUser;
1035
1036 ASMAtomicDecU32(&pThis->StatIoDepth);
1037
1038 /* Sync buffers. */
1039 size_t cbCopyIn = RT_MIN(pReq->cbBuf, cbXfer);
1040 if ( RT_SUCCESS(rcReq)
1041 && cbCopyIn
1042 && ( pReq->enmXferDir == PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN
1043 || pReq->enmXferDir == PDMMEDIAEXIOREQSCSITXDIR_FROM_DEVICE))
1044 {
1045 RTSGBUF SgBuf;
1046 RTSgBufInit(&SgBuf, &pReq->Seg, 1);
1047 int rcCopy = pThis->pDevMediaExPort->pfnIoReqCopyFromBuf(pThis->pDevMediaExPort, (PDMMEDIAEXIOREQ)pReq,
1048 &pReq->abAlloc[0], 0, &SgBuf, cbCopyIn);
1049 if (RT_FAILURE(rcCopy))
1050 rcReq = rcCopy;
1051 }
1052
1053 if (pReq->pvBuf)
1054 {
1055 RTMemFree(pReq->pvBuf);
1056 pReq->pvBuf = NULL;
1057 }
1058
1059 *pReq->pu8ScsiSts = (uint8_t)rcScsiCode;
1060 pReq->cbXfer = cbXfer;
1061 if (pReq->pcbSense)
1062 *pReq->pcbSense = cbSense;
1063 if (pReq->penmXferDir)
1064 *pReq->penmXferDir = drvscsiVScsiXferDir2PdmMediaExDir(enmXferDir);
1065 int rc = pThis->pDevMediaExPort->pfnIoReqCompleteNotify(pThis->pDevMediaExPort, (PDMMEDIAEXIOREQ)pReq,
1066 &pReq->abAlloc[0], rcReq);
1067 AssertRC(rc); RT_NOREF(rc);
1068
1069 if (RT_UNLIKELY(pThis->fDummySignal) && !pThis->StatIoDepth)
1070 PDMDrvHlpAsyncNotificationCompleted(pThis->pDrvIns);
1071}
1072
1073/**
1074 * Consumer for the queue
1075 *
1076 * @returns Success indicator.
1077 * If false the item will not be removed and the flushing will stop.
1078 * @param pDrvIns The driver instance.
1079 * @param pItem The item to consume. Upon return this item will be freed.
1080 * @thread EMT
1081 *
1082 * @todo r=bird: Seems the idea here is that we have to do this on an EMT,
1083 * probably because of PDMIMOUNT::pfnUnmount. I don't quite get why
1084 * though, as EMT doesn't exactly serialize anything anymore (SMP)...
1085 */
1086static DECLCALLBACK(bool) drvscsiR3NotifyQueueConsumer(PPDMDRVINS pDrvIns, PPDMQUEUEITEMCORE pItem)
1087{
1088 PDRVSCSIEJECTSTATE pEjectState = (PDRVSCSIEJECTSTATE)pItem;
1089 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1090
1091 int rc = pThis->pDrvMount->pfnUnmount(pThis->pDrvMount, false /*fForce*/, true /*fEject*/);
1092 Assert(RT_SUCCESS(rc) || rc == VERR_PDM_MEDIA_LOCKED || rc == VERR_PDM_MEDIA_NOT_MOUNTED);
1093 if (RT_SUCCESS(rc))
1094 pThis->pDevMediaExPort->pfnMediumEjected(pThis->pDevMediaExPort);
1095
1096 pEjectState->rcReq = rc;
1097 RTSemEventSignal(pEjectState->hSemEvt);
1098 return true;
1099}
1100
1101/* -=-=-=-=- IBase -=-=-=-=- */
1102
1103/**
1104 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
1105 */
1106static DECLCALLBACK(void *) drvscsiQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1107{
1108 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
1109 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1110
1111 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUNT, pThis->pDrvMount);
1112 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
1113 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEX, pThis->pDevMediaExPort ? &pThis->IMediaEx : NULL);
1114 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIA, pThis->pDrvMedia ? &pThis->IMedia : NULL);
1115 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pThis->IPort);
1116 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMOUNTNOTIFY, &pThis->IMountNotify);
1117 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pThis->IPortEx);
1118 return NULL;
1119}
1120
1121static DECLCALLBACK(int) drvscsiQueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
1122 uint32_t *piInstance, uint32_t *piLUN)
1123{
1124 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IPort);
1125
1126 return pThis->pDevMediaPort->pfnQueryDeviceLocation(pThis->pDevMediaPort, ppcszController,
1127 piInstance, piLUN);
1128}
1129
1130/**
1131 * Called when media is mounted.
1132 *
1133 * @param pInterface Pointer to the interface structure containing the called function pointer.
1134 */
1135static DECLCALLBACK(void) drvscsiMountNotify(PPDMIMOUNTNOTIFY pInterface)
1136{
1137 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMountNotify);
1138 LogFlowFunc(("mounting LUN#%p\n", pThis->hVScsiLun));
1139
1140 /* Ignore the call if we're called while being attached. */
1141 if (!pThis->pDrvMedia)
1142 return;
1143
1144 /* Let the LUN know that a medium was mounted. */
1145 VSCSILunMountNotify(pThis->hVScsiLun);
1146}
1147
1148/**
1149 * Called when media is unmounted
1150 *
1151 * @param pInterface Pointer to the interface structure containing the called function pointer.
1152 */
1153static DECLCALLBACK(void) drvscsiUnmountNotify(PPDMIMOUNTNOTIFY pInterface)
1154{
1155 PDRVSCSI pThis = RT_FROM_MEMBER(pInterface, DRVSCSI, IMountNotify);
1156 LogFlowFunc(("unmounting LUN#%p\n", pThis->hVScsiLun));
1157
1158 /* Let the LUN know that the medium was unmounted. */
1159 VSCSILunUnmountNotify(pThis->hVScsiLun);
1160}
1161
1162/**
1163 * Worker for drvscsiReset, drvscsiSuspend and drvscsiPowerOff.
1164 *
1165 * @param pDrvIns The driver instance.
1166 * @param pfnAsyncNotify The async callback.
1167 */
1168static void drvscsiR3ResetOrSuspendOrPowerOff(PPDMDRVINS pDrvIns, PFNPDMDRVASYNCNOTIFY pfnAsyncNotify)
1169{
1170 RT_NOREF1(pfnAsyncNotify);
1171
1172 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1173
1174 if (pThis->StatIoDepth > 0)
1175 ASMAtomicWriteBool(&pThis->fDummySignal, true);
1176}
1177
1178/**
1179 * Callback employed by drvscsiSuspend and drvscsiPowerOff.
1180 *
1181 * @returns true if we've quiesced, false if we're still working.
1182 * @param pDrvIns The driver instance.
1183 */
1184static DECLCALLBACK(bool) drvscsiIsAsyncSuspendOrPowerOffDone(PPDMDRVINS pDrvIns)
1185{
1186 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1187
1188 if (pThis->StatIoDepth > 0)
1189 return false;
1190 else
1191 return true;
1192}
1193
1194/**
1195 * @copydoc FNPDMDRVPOWEROFF
1196 */
1197static DECLCALLBACK(void) drvscsiPowerOff(PPDMDRVINS pDrvIns)
1198{
1199 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
1200}
1201
1202/**
1203 * @copydoc FNPDMDRVSUSPEND
1204 */
1205static DECLCALLBACK(void) drvscsiSuspend(PPDMDRVINS pDrvIns)
1206{
1207 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncSuspendOrPowerOffDone);
1208}
1209
1210/**
1211 * Callback employed by drvscsiReset.
1212 *
1213 * @returns true if we've quiesced, false if we're still working.
1214 * @param pDrvIns The driver instance.
1215 */
1216static DECLCALLBACK(bool) drvscsiIsAsyncResetDone(PPDMDRVINS pDrvIns)
1217{
1218 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1219
1220 if (pThis->StatIoDepth > 0)
1221 return false;
1222 else
1223 return true;
1224}
1225
1226/** @copydoc FNPDMDRVATTACH */
1227static DECLCALLBACK(int) drvscsiAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1228{
1229 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1230
1231 LogFlowFunc(("pDrvIns=%#p fFlags=%#x\n", pDrvIns, fFlags));
1232
1233 AssertMsgReturn((fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG),
1234 ("SCSI: Hotplugging is not supported\n"),
1235 VERR_INVALID_PARAMETER);
1236
1237 /*
1238 * Try attach driver below and query it's media interface.
1239 */
1240 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pThis->pDrvBase);
1241 AssertMsgReturn(RT_SUCCESS(rc), ("Attaching driver below failed rc=%Rrc\n", rc), rc);
1242
1243 /*
1244 * Query the media interface.
1245 */
1246 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMEDIA);
1247 AssertMsgReturn(RT_VALID_PTR(pThis->pDrvMedia), ("VSCSI configuration error: No media interface!\n"),
1248 VERR_PDM_MISSING_INTERFACE);
1249
1250 /* Query the extended media interface. */
1251 pThis->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMEDIAEX);
1252 AssertMsgReturn(RT_VALID_PTR(pThis->pDrvMediaEx), ("VSCSI configuration error: No extended media interface!\n"),
1253 VERR_PDM_MISSING_INTERFACE);
1254
1255 pThis->pDrvMount = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMOUNT);
1256
1257 if (pThis->cbVScsiIoReqAlloc)
1258 {
1259 rc = pThis->pDrvMediaEx->pfnIoReqAllocSizeSet(pThis->pDrvMediaEx, pThis->cbVScsiIoReqAlloc);
1260 AssertMsgReturn(RT_SUCCESS(rc), ("Setting the I/O request allocation size failed with rc=%Rrc\n", rc), rc);
1261 }
1262
1263 if (pThis->pDrvMount)
1264 {
1265 if (pThis->pDrvMount->pfnIsMounted(pThis->pDrvMount))
1266 {
1267 rc = VINF_SUCCESS; VSCSILunMountNotify(pThis->hVScsiLun);
1268 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to notify the LUN of media being mounted\n"), rc);
1269 }
1270 else
1271 {
1272 rc = VINF_SUCCESS; VSCSILunUnmountNotify(pThis->hVScsiLun);
1273 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to notify the LUN of media being unmounted\n"), rc);
1274 }
1275 }
1276
1277 return rc;
1278}
1279
1280/** @copydoc FNPDMDRVDETACH */
1281static DECLCALLBACK(void) drvscsiDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
1282{
1283 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1284
1285 RT_NOREF(fFlags);
1286 LogFlowFunc(("pDrvIns=%#p fFlags=%#x\n", pDrvIns, fFlags));
1287
1288 /*
1289 * Zero some important members.
1290 */
1291 pThis->pDrvBase = NULL;
1292 pThis->pDrvMedia = NULL;
1293 pThis->pDrvMediaEx = NULL;
1294 pThis->pDrvMount = NULL;
1295
1296 VSCSILunUnmountNotify(pThis->hVScsiLun);
1297}
1298
1299/**
1300 * @copydoc FNPDMDRVRESET
1301 */
1302static DECLCALLBACK(void) drvscsiReset(PPDMDRVINS pDrvIns)
1303{
1304 drvscsiR3ResetOrSuspendOrPowerOff(pDrvIns, drvscsiIsAsyncResetDone);
1305}
1306
1307/**
1308 * Destruct a driver instance.
1309 *
1310 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
1311 * resources can be freed correctly.
1312 *
1313 * @param pDrvIns The driver instance data.
1314 */
1315static DECLCALLBACK(void) drvscsiDestruct(PPDMDRVINS pDrvIns)
1316{
1317 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1318 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
1319
1320 /* Free the VSCSI device and LUN handle. */
1321 if (pThis->hVScsiDevice)
1322 {
1323 VSCSILUN hVScsiLun;
1324 int rc = VSCSIDeviceLunDetach(pThis->hVScsiDevice, 0, &hVScsiLun);
1325 AssertRC(rc);
1326
1327 Assert(hVScsiLun == pThis->hVScsiLun);
1328 rc = VSCSILunDestroy(hVScsiLun);
1329 AssertRC(rc);
1330 rc = VSCSIDeviceDestroy(pThis->hVScsiDevice);
1331 AssertRC(rc);
1332
1333 pThis->hVScsiDevice = NULL;
1334 pThis->hVScsiLun = NULL;
1335 }
1336}
1337
1338/**
1339 * Construct a block driver instance.
1340 *
1341 * @copydoc FNPDMDRVCONSTRUCT
1342 */
1343static DECLCALLBACK(int) drvscsiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
1344{
1345 RT_NOREF(pCfg);
1346 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
1347 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
1348 LogFlowFunc(("pDrvIns=%#p pCfg=%#p\n", pDrvIns, pCfg));
1349
1350 /*
1351 * Initialize the instance data.
1352 */
1353 pThis->pDrvIns = pDrvIns;
1354
1355 pDrvIns->IBase.pfnQueryInterface = drvscsiQueryInterface;
1356
1357 /* IMedia */
1358 pThis->IMedia.pfnRead = NULL;
1359 pThis->IMedia.pfnReadPcBios = NULL;
1360 pThis->IMedia.pfnWrite = NULL;
1361 pThis->IMedia.pfnFlush = NULL;
1362 pThis->IMedia.pfnSendCmd = NULL;
1363 pThis->IMedia.pfnMerge = NULL;
1364 pThis->IMedia.pfnSetSecKeyIf = NULL;
1365 pThis->IMedia.pfnGetSize = drvscsiGetSize;
1366 pThis->IMedia.pfnGetSectorSize = drvscsiGetSectorSize;
1367 pThis->IMedia.pfnIsReadOnly = drvscsiIsReadOnly;
1368 pThis->IMedia.pfnIsNonRotational = drvscsiIsNonRotational;
1369 pThis->IMedia.pfnBiosGetPCHSGeometry = drvscsiBiosGetPCHSGeometry;
1370 pThis->IMedia.pfnBiosSetPCHSGeometry = drvscsiBiosSetPCHSGeometry;
1371 pThis->IMedia.pfnBiosGetLCHSGeometry = drvscsiBiosGetLCHSGeometry;
1372 pThis->IMedia.pfnBiosSetLCHSGeometry = drvscsiBiosSetLCHSGeometry;
1373 pThis->IMedia.pfnBiosIsVisible = drvscsiBiosIsVisible;
1374 pThis->IMedia.pfnGetType = drvscsiGetType;
1375 pThis->IMedia.pfnGetUuid = drvscsiGetUuid;
1376 pThis->IMedia.pfnDiscard = NULL;
1377
1378 /* IMediaEx */
1379 pThis->IMediaEx.pfnQueryFeatures = drvscsiQueryFeatures;
1380 pThis->IMediaEx.pfnNotifySuspend = drvscsiNotifySuspend;
1381 pThis->IMediaEx.pfnIoReqAllocSizeSet = drvscsiIoReqAllocSizeSet;
1382 pThis->IMediaEx.pfnIoReqAlloc = drvscsiIoReqAlloc;
1383 pThis->IMediaEx.pfnIoReqFree = drvscsiIoReqFree;
1384 pThis->IMediaEx.pfnIoReqQueryResidual = drvscsiIoReqQueryResidual;
1385 pThis->IMediaEx.pfnIoReqQueryXferSize = drvscsiIoReqQueryXferSize;
1386 pThis->IMediaEx.pfnIoReqCancelAll = drvscsiIoReqCancelAll;
1387 pThis->IMediaEx.pfnIoReqCancel = drvscsiIoReqCancel;
1388 pThis->IMediaEx.pfnIoReqRead = drvscsiIoReqRead;
1389 pThis->IMediaEx.pfnIoReqWrite = drvscsiIoReqWrite;
1390 pThis->IMediaEx.pfnIoReqFlush = drvscsiIoReqFlush;
1391 pThis->IMediaEx.pfnIoReqDiscard = drvscsiIoReqDiscard;
1392 pThis->IMediaEx.pfnIoReqSendScsiCmd = drvscsiIoReqSendScsiCmd;
1393 pThis->IMediaEx.pfnIoReqGetActiveCount = drvscsiIoReqGetActiveCount;
1394 pThis->IMediaEx.pfnIoReqGetSuspendedCount = drvscsiIoReqGetSuspendedCount;
1395 pThis->IMediaEx.pfnIoReqQuerySuspendedStart = drvscsiIoReqQuerySuspendedStart;
1396 pThis->IMediaEx.pfnIoReqQuerySuspendedNext = drvscsiIoReqQuerySuspendedNext;
1397 pThis->IMediaEx.pfnIoReqSuspendedSave = drvscsiIoReqSuspendedSave;
1398 pThis->IMediaEx.pfnIoReqSuspendedLoad = drvscsiIoReqSuspendedLoad;
1399
1400 pThis->IMountNotify.pfnMountNotify = drvscsiMountNotify;
1401 pThis->IMountNotify.pfnUnmountNotify = drvscsiUnmountNotify;
1402 pThis->IPort.pfnQueryDeviceLocation = drvscsiQueryDeviceLocation;
1403 pThis->IPortEx.pfnIoReqCompleteNotify = drvscsiIoReqCompleteNotify;
1404 pThis->IPortEx.pfnIoReqCopyFromBuf = drvscsiIoReqCopyFromBuf;
1405 pThis->IPortEx.pfnIoReqCopyToBuf = drvscsiIoReqCopyToBuf;
1406 pThis->IPortEx.pfnIoReqQueryBuf = NULL;
1407 pThis->IPortEx.pfnIoReqQueryDiscardRanges = drvscsiIoReqQueryDiscardRanges;
1408 pThis->IPortEx.pfnIoReqStateChanged = drvscsiIoReqStateChanged;
1409
1410 /* Query the optional media port interface above. */
1411 pThis->pDevMediaPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAPORT);
1412
1413 /* Query the optional extended media port interface above. */
1414 pThis->pDevMediaExPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIMEDIAEXPORT);
1415
1416 AssertMsgReturn(pThis->pDevMediaExPort,
1417 ("Missing extended media port interface above\n"), VERR_PDM_MISSING_INTERFACE);
1418
1419 /* Query the optional LED interface above. */
1420 pThis->pLedPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMILEDPORTS);
1421 if (pThis->pLedPort != NULL)
1422 {
1423 /* Get The Led. */
1424 int rc = pThis->pLedPort->pfnQueryStatusLed(pThis->pLedPort, 0, &pThis->pLed);
1425 if (RT_FAILURE(rc))
1426 pThis->pLed = &pThis->Led;
1427 }
1428 else
1429 pThis->pLed = &pThis->Led;
1430
1431 /*
1432 * Validate and read configuration.
1433 */
1434 PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "", "");
1435
1436 /*
1437 * Try attach driver below and query it's media interface.
1438 */
1439 int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pThis->pDrvBase);
1440 if (RT_FAILURE(rc))
1441 return rc;
1442
1443 /*
1444 * Query the media interface.
1445 */
1446 pThis->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMEDIA);
1447 AssertMsgReturn(RT_VALID_PTR(pThis->pDrvMedia), ("VSCSI configuration error: No media interface!\n"),
1448 VERR_PDM_MISSING_INTERFACE);
1449
1450 /* Query the extended media interface. */
1451 pThis->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMEDIAEX);
1452 AssertMsgReturn(RT_VALID_PTR(pThis->pDrvMediaEx), ("VSCSI configuration error: No extended media interface!\n"),
1453 VERR_PDM_MISSING_INTERFACE);
1454
1455 pThis->pDrvMount = PDMIBASE_QUERY_INTERFACE(pThis->pDrvBase, PDMIMOUNT);
1456
1457 PDMMEDIATYPE enmType = pThis->pDrvMedia->pfnGetType(pThis->pDrvMedia);
1458 VSCSILUNTYPE enmLunType;
1459 switch (enmType)
1460 {
1461 case PDMMEDIATYPE_HARD_DISK:
1462 enmLunType = VSCSILUNTYPE_SBC;
1463 break;
1464 case PDMMEDIATYPE_CDROM:
1465 case PDMMEDIATYPE_DVD:
1466 enmLunType = VSCSILUNTYPE_MMC;
1467 break;
1468 default:
1469 return PDMDrvHlpVMSetError(pDrvIns, VERR_PDM_UNSUPPORTED_BLOCK_TYPE, RT_SRC_POS,
1470 N_("Only hard disks and CD/DVD-ROMs are currently supported as SCSI devices (enmType=%d)"),
1471 enmType);
1472 }
1473 if ( ( enmType == PDMMEDIATYPE_DVD
1474 || enmType == PDMMEDIATYPE_CDROM)
1475 && !pThis->pDrvMount)
1476 {
1477 AssertMsgFailed(("Internal error: cdrom without a mountable interface\n"));
1478 return VERR_INTERNAL_ERROR;
1479 }
1480
1481 /* Create VSCSI device and LUN. */
1482 pThis->VScsiIoCallbacks.pfnVScsiLunReqAllocSizeSet = drvscsiReqAllocSizeSet;
1483 pThis->VScsiIoCallbacks.pfnVScsiLunReqAlloc = drvscsiReqAlloc;
1484 pThis->VScsiIoCallbacks.pfnVScsiLunReqFree = drvscsiReqFree;
1485 pThis->VScsiIoCallbacks.pfnVScsiLunMediumGetRegionCount = drvscsiGetRegionCount;
1486 pThis->VScsiIoCallbacks.pfnVScsiLunMediumQueryRegionProperties = drvscsiQueryRegionProperties;
1487 pThis->VScsiIoCallbacks.pfnVScsiLunMediumQueryRegionPropertiesForLba = drvscsiQueryRegionPropertiesForLba;
1488 pThis->VScsiIoCallbacks.pfnVScsiLunMediumEject = drvscsiEject;
1489 pThis->VScsiIoCallbacks.pfnVScsiLunReqTransferEnqueue = drvscsiReqTransferEnqueue;
1490 pThis->VScsiIoCallbacks.pfnVScsiLunGetFeatureFlags = drvscsiGetFeatureFlags;
1491 pThis->VScsiIoCallbacks.pfnVScsiLunMediumSetLock = drvscsiSetLock;
1492 pThis->VScsiIoCallbacks.pfnVScsiLunQueryInqStrings = drvscsiQueryInqStrings;
1493
1494 rc = VSCSIDeviceCreate(&pThis->hVScsiDevice, drvscsiIoReqVScsiReqCompleted, pThis);
1495 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI device rc=%Rrc\n", rc), rc);
1496 rc = VSCSILunCreate(&pThis->hVScsiLun, enmLunType, &pThis->VScsiIoCallbacks,
1497 pThis);
1498 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create VSCSI LUN rc=%Rrc\n", rc), rc);
1499 rc = VSCSIDeviceLunAttach(pThis->hVScsiDevice, pThis->hVScsiLun, 0);
1500 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to attached the LUN to the SCSI device\n"), rc);
1501
1502 /// @todo This is a very hacky way of telling the LUN whether a medium was mounted.
1503 // The mount/unmount interface doesn't work in a very sensible manner!
1504 if (pThis->pDrvMount)
1505 {
1506 if (pThis->pDrvMount->pfnIsMounted(pThis->pDrvMount))
1507 {
1508 rc = VINF_SUCCESS; VSCSILunMountNotify(pThis->hVScsiLun);
1509 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to notify the LUN of media being mounted\n"), rc);
1510 }
1511 else
1512 {
1513 rc = VINF_SUCCESS; VSCSILunUnmountNotify(pThis->hVScsiLun);
1514 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to notify the LUN of media being unmounted\n"), rc);
1515 }
1516 }
1517
1518 uint32_t fFeatures = 0;
1519 rc = pThis->pDrvMediaEx->pfnQueryFeatures(pThis->pDrvMediaEx, &fFeatures);
1520 if (RT_FAILURE(rc))
1521 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1522 N_("VSCSI configuration error: Failed to query features of device"));
1523 if (fFeatures & PDMIMEDIAEX_FEATURE_F_DISCARD)
1524 LogRel(("SCSI#%d: Enabled UNMAP support\n", pDrvIns->iInstance));
1525
1526 rc = PDMDrvHlpQueueCreate(pDrvIns, sizeof(DRVSCSIEJECTSTATE), 1, 0, drvscsiR3NotifyQueueConsumer,
1527 "SCSI-Eject", &pThis->hQueue);
1528 if (RT_FAILURE(rc))
1529 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
1530 N_("VSCSI configuration error: Failed to create notification queue"));
1531
1532 return VINF_SUCCESS;
1533}
1534
1535/**
1536 * SCSI driver registration record.
1537 */
1538const PDMDRVREG g_DrvSCSI =
1539{
1540 /* u32Version */
1541 PDM_DRVREG_VERSION,
1542 /* szName */
1543 "SCSI",
1544 /* szRCMod */
1545 "",
1546 /* szR0Mod */
1547 "",
1548 /* pszDescription */
1549 "Generic SCSI driver.",
1550 /* fFlags */
1551 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
1552 /* fClass. */
1553 PDM_DRVREG_CLASS_SCSI,
1554 /* cMaxInstances */
1555 ~0U,
1556 /* cbInstance */
1557 sizeof(DRVSCSI),
1558 /* pfnConstruct */
1559 drvscsiConstruct,
1560 /* pfnDestruct */
1561 drvscsiDestruct,
1562 /* pfnRelocate */
1563 NULL,
1564 /* pfnIOCtl */
1565 NULL,
1566 /* pfnPowerOn */
1567 NULL,
1568 /* pfnReset */
1569 drvscsiReset,
1570 /* pfnSuspend */
1571 drvscsiSuspend,
1572 /* pfnResume */
1573 NULL,
1574 /* pfnAttach */
1575 drvscsiAttach,
1576 /* pfnDetach */
1577 drvscsiDetach,
1578 /* pfnPowerOff */
1579 drvscsiPowerOff,
1580 /* pfnSoftReset */
1581 NULL,
1582 /* u32EndVersion */
1583 PDM_DRVREG_VERSION
1584};
Note: See TracBrowser for help on using the repository browser.

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