VirtualBox

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

Last change on this file since 78374 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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