Index: trunk/src/VBox/Devices/Storage/DevVirtioSCSI.cpp
===================================================================
--- trunk/src/VBox/Devices/Storage/DevVirtioSCSI.cpp	(revision 82145)
+++ trunk/src/VBox/Devices/Storage/DevVirtioSCSI.cpp	(revision 82151)
@@ -334,4 +334,6 @@
      | VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY )
 
+#define SUPPORTED_EVENTS            0                           /* TBD */
+
 /**
  * Worker thread context, shared state.
@@ -372,5 +374,5 @@
 
     /** Target number (PDM LUN) */
-    uint32_t                        iTarget;
+    uint32_t                        uTarget;
 
     /** Target Description */
@@ -391,6 +393,4 @@
     PDMIMEDIAEXPORT                 IMediaExPort;
 
-    PPDMIMEDIANOTIFY                pMediaNotify;
-
     /** Pointer to the attached driver's extended media interface. */
     R3PTRTYPE(PPDMIMEDIAEX)         pDrvMediaEx;
@@ -443,7 +443,4 @@
     /** Total number of requests active across all targets */
     volatile uint32_t               cActiveReqs;
-
-    /** Events the guest has subscribed to get notifications of */
-    uint32_t                        fSubscribedEvents;
 
     /** VirtIO Host SCSI device runtime configuration parameters */
@@ -734,6 +731,8 @@
     AssertReturn(paReqSegs, VERR_NO_MEMORY);
 
-    paReqSegs[0].pvSeg = &event;
-    paReqSegs[0].cbSeg = sizeof(sizeof(event));
+    /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
+    paReqSegs[0].pvSeg = RTMemAlloc(sizeof(event));
+    AssertReturn(paReqSegs[0].pvSeg, VERR_NO_MEMORY);
+    memcpy(paReqSegs[0].pvSeg, &event, sizeof(event));
 
     RTSgBufInit(pReqSegBuf, paReqSegs, 1);
@@ -742,4 +741,5 @@
     virtioCoreQueueSync(pDevIns, &pThis->Virtio, EVENTQ_IDX);
 
+    RTMemFree(paReqSegs[0].pvSeg);
     RTMemFree(paReqSegs);
     RTMemFree(pReqSegBuf);
@@ -793,4 +793,13 @@
         pRespHdr->cbSenseLen = 0;
 
+    /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
+    for (int i = 0; i < 2; i++)
+    {
+        void *pv = paReqSegs[i].pvSeg;
+        paReqSegs[i].pvSeg = RTMemAlloc(paReqSegs[i].cbSeg);
+        AssertReturn(paReqSegs[i].pvSeg, VERR_NO_MEMORY);
+        memcpy(paReqSegs[i].pvSeg, pv, paReqSegs[i].cbSeg);
+    }
+
     RTSgBufInit(pReqSegBuf, paReqSegs, 2);
 
@@ -801,7 +810,10 @@
     virtioCoreQueueSync(pDevIns, &pThis->Virtio, qIdx);
 
-    RTMemFree(pabSenseBuf);
+    for (int i = 0; i < 2; i++)
+        RTMemFree(paReqSegs[i].pvSeg);
+
     RTMemFree(paReqSegs);
     RTMemFree(pReqSegBuf);
+    RTMemFree(pabSenseBuf);
 
     if (!ASMAtomicDecU32(&pThis->cActiveReqs) && pThisCC->fQuiescing)
@@ -951,4 +963,13 @@
         paReqSegs[cSegs++].cbSeg = pReq->cbSenseAlloc; /* VirtIO 1.0 spec 5.6.4/5.6.6.1 */
 
+        /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
+        for (int i = 0; i < cSegs; i++)
+        {
+            void *pv = paReqSegs[i].pvSeg;
+            paReqSegs[i].pvSeg = RTMemAlloc(paReqSegs[i].cbSeg);
+            AssertReturn(paReqSegs[i].pvSeg, VERR_NO_MEMORY);
+            memcpy(paReqSegs[i].pvSeg, pv, paReqSegs[i].cbSeg);
+        }
+
         RTSgBufInit(pReqSegBuf, paReqSegs, cSegs);
 
@@ -961,4 +982,7 @@
         virtioCoreR3QueuePut(pDevIns, &pThis->Virtio, pReq->qIdx, pReqSegBuf, pReq->pDescChain, true /* fFence TBD */);
         virtioCoreQueueSync(pDevIns, &pThis->Virtio, pReq->qIdx);
+
+        for (int i = 0; i < cSegs; i++)
+            RTMemFree(paReqSegs[i].pvSeg);
 
         RTMemFree(paReqSegs);
@@ -1299,5 +1323,5 @@
 {
     uint8_t bResponse = VIRTIOSCSI_S_OK;
-    uint8_t cSegs = 1;
+    uint8_t cSegs = 0;
 
     AssertReturn(pDescChain->cbPhysSend >= RT_MIN(sizeof(VIRTIOSCSI_CTRL_AN_T),
@@ -1325,13 +1349,4 @@
                        || pScsiCtrlUnion->scsiCtrl.uType == VIRTIOSCSI_T_AN_SUBSCRIBE)
                      && pDescChain->cbPhysSend >= sizeof(VIRTIOSCSI_CTRL_AN_T)), 0);
-
-    /*
-     * Mask of events to tell guest driver this device supports
-     * See VirtIO 1.0 specification section 5.6.6.2
-     */
-    uint32_t fSubscribedEvents  = VIRTIOSCSI_EVT_ASYNC_POWER_MGMT
-                                | VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST
-                                | VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE
-                                | VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY;
 
     PRTSGSEG paReqSegs = (PRTSGSEG)RTMemAllocZ(sizeof(RTSGSEG) * 2);
@@ -1387,5 +1402,5 @@
             RTSGSEG aSegs[] = { { &bResponse,  sizeof(bResponse) } };
             memcpy(paReqSegs, aSegs, sizeof(aSegs));
-
+            cSegs = RT_ELEMENTS(aSegs);
             break;
         }
@@ -1394,6 +1409,4 @@
 
             PVIRTIOSCSI_CTRL_AN_T pScsiCtrlAnQuery = &pScsiCtrlUnion->scsiCtrlAsyncNotify;
-
-            fSubscribedEvents &= pScsiCtrlAnQuery->fEventsRequested;
 
             uint8_t  uTarget  = pScsiCtrlAnQuery->abScsiLun[1];
@@ -1417,21 +1430,18 @@
             }
 #endif
-            RTSGSEG aSegs[] = { { &fSubscribedEvents, sizeof(fSubscribedEvents) },
+            uint32_t fSupportedEvents = SUPPORTED_EVENTS;
+            RTSGSEG aSegs[] = { { &fSupportedEvents, sizeof(fSupportedEvents) },
                                 { &bResponse, sizeof(bResponse) } };
-            cSegs = 2;
             memcpy(paReqSegs, aSegs, sizeof(aSegs));
-
+            cSegs = RT_ELEMENTS(aSegs);
             break;
         }
         case VIRTIOSCSI_T_AN_SUBSCRIBE: /* Guest SCSI driver is subscribing to async event notification(s) */
         {
-
             PVIRTIOSCSI_CTRL_AN_T pScsiCtrlAnSubscribe = &pScsiCtrlUnion->scsiCtrlAsyncNotify;
 
             if (pScsiCtrlAnSubscribe->fEventsRequested & ~SUBSCRIBABLE_EVENTS)
-                LogFunc(("Unsupported bits in event subscription event mask: %#x\n", pScsiCtrlAnSubscribe->fEventsRequested));
-
-            fSubscribedEvents &= pScsiCtrlAnSubscribe->fEventsRequested;
-            pThis->fAsyncEvtsEnabled = fSubscribedEvents;
+                LogFunc(("Unsupported bits in event subscription event mask: %#x\n",
+                         pScsiCtrlAnSubscribe->fEventsRequested));
 
             uint8_t  uTarget  = pScsiCtrlAnSubscribe->abScsiLun[1];
@@ -1455,18 +1465,12 @@
             else
             {
-                /*
-                 * TBD: Verify correct status code if request mask is only partially fulfillable
-                 *      and confirm when to use 'complete' vs. 'succeeded' See VirtIO 1.0 spec section 5.6.6.2
-                 *      and read SAM docs*/
-                if (fSubscribedEvents == pScsiCtrlAnSubscribe->fEventsRequested)
-                    bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
-                else
-                    bResponse = VIRTIOSCSI_S_FUNCTION_COMPLETE;
+                bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED; /* or VIRTIOSCSI_S_FUNCTION_COMPLETE? */
+                pThis->fAsyncEvtsEnabled = SUPPORTED_EVENTS & pScsiCtrlAnSubscribe->fEventsRequested;
             }
-            RTSGSEG aSegs[] = { { &fSubscribedEvents, sizeof(fSubscribedEvents) },
+
+            RTSGSEG aSegs[] = { { &pThis->fAsyncEvtsEnabled, sizeof(pThis->fAsyncEvtsEnabled) },
                                 { &bResponse, sizeof(bResponse) } };
-            cSegs = 2;
             memcpy(paReqSegs, aSegs, sizeof(aSegs));
-
+            cSegs = RT_ELEMENTS(aSegs);
             break;
         }
@@ -1478,5 +1482,5 @@
             RTSGSEG aSegs[] = { { &bResponse, sizeof(bResponse) } };
             memcpy(paReqSegs, aSegs, sizeof(aSegs));
-
+            cSegs = RT_ELEMENTS(aSegs);
         }
     }
@@ -1486,8 +1490,20 @@
     AssertReturn(pReqSegBuf, VERR_NO_MEMORY);
 
+    /* Copy segment data to malloc'd memory to avoid stack out-of-scope errors sanitizer doesn't detect */
+    for (int i = 0; i < cSegs; i++)
+    {
+        void *pv = paReqSegs[i].pvSeg;
+        paReqSegs[i].pvSeg = RTMemAlloc(paReqSegs[i].cbSeg);
+        AssertReturn(paReqSegs[i].pvSeg, VERR_NO_MEMORY);
+        memcpy(paReqSegs[i].pvSeg, pv, paReqSegs[i].cbSeg);
+    }
+
     RTSgBufInit(pReqSegBuf, paReqSegs, cSegs);
 
     virtioCoreR3QueuePut(pDevIns, &pThis->Virtio, qIdx, pReqSegBuf, pDescChain, true);
     virtioCoreQueueSync(pDevIns, &pThis->Virtio, qIdx);
+
+    for (int i = 0; i < cSegs; i++)
+        RTMemFree(paReqSegs[i].pvSeg);
 
     RTMemFree(paReqSegs);
@@ -1596,10 +1612,30 @@
 }
 
+
 #if 0
-
-/** Only invoke this if VIRTIOSCSI_F_HOTPLUG is negotiated during intiailization
- * This effectively removes the SCSI Target/LUN on the guest side
- */
-DECLINLINE(void) virtioScsiR3ReportTargetRemoved(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
+/* SUBSCRIBABLE EVENT - not sure when to call this or how to detect when media is added or removed
+ *                      via the VBox GUI */
+DECLINLINE(void) virtioScsiR3ReportMediaChange(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
+{
+    if (pThis->fAsyncEvtsEnabled & VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE)
+        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE);
+}
+
+/* ESSENTIAL (NON-SUBSCRIBABLE) EVENT TYPES (most guest virtio-scsi drivers ignore?)  */
+
+DECLINLINE(void) virtioScsiR3ReportTransportReset(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
+{
+    virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_HARD);
+}
+
+DECLINLINE(void) virtioScsiR3ReportParamChange(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget,
+                                               uint32_t uSenseCode, uint32_t uSenseQualifier)
+{
+    uint32_t uReason = uSenseQualifier << 8 | uSenseCode;
+    virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_PARAM_CHANGE, uReason);
+
+}
+
+DECLINLINE(void) virtioScsiR3ReportTargetRemoved(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
 {
     if (pThis->fHasHotplug)
@@ -1607,60 +1643,8 @@
 }
 
-/** Only invoke this if VIRTIOSCSI_F_HOTPLUG is negotiated during intiailization
- * This effectively adds the SCSI Target/LUN on the guest side
- */
-DECLINLINE(void) virtioScsiR3ReportTargetAdded(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
+DECLINLINE(void) virtioScsiR3ReportTargetAdded(PDMDEVINS pDevInsPVIRTIOSCSI pThis, uint16_t uTarget)
 {
     if (pThis->fHasHotplug)
         virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_RESCAN);
-}
-
-DECLINLINE(void) virtioScsiR3ReportTargetReset(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_HARD);
-}
-
-DECLINLINE(void) virtioScsiR3ReportOperChange(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_OPERATIONAL_CHANGE)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_OPERATIONAL_CHANGE);
-}
-
-DECLINLINE(void) virtioScsiR3ReportPowerMsg(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_POWER_MGMT)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_POWER_MGMT);
-}
-
-DECLINLINE(void) virtioScsiR3ReportExtReq(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST);
-}
-
-DECLINLINE(void) virtioScsiR3ReportMediaChange(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE);
-}
-
-DECLINLINE(void) virtioScsiR3ReportMultiHost(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_MULTI_HOST)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_MULTI_HOST);
-}
-
-DECLINLINE(void) virtioScsiR3ReportDeviceBusy(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, PVIRTIOSCSI pThis, uint16_t uTarget)
-{
-    if (pThis->fSubscribedEvents & VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY)
-        virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY);
-}
-
-DECLINLINE(void) virtioScsiR3ReportParamChange(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget,
-                                               uint32_t uSenseCode, uint32_t uSenseQualifier)
-{
-    uint32_t uReason = uSenseQualifier << 8 | uSenseCode;
-    virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_PARAM_CHANGE, uReason);
-
 }
 
@@ -1758,5 +1742,4 @@
     return VERR_PDM_LUN_NOT_FOUND;
 }
-
 /**
  * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed, Device level.}
@@ -1795,5 +1778,5 @@
     *ppcszController = pDevIns->pReg->szName;
     *piInstance = pDevIns->iInstance;
-    *piLUN = pTarget->iTarget;
+    *piLUN = pTarget->uTarget;
 
     return VINF_SUCCESS;
@@ -1861,23 +1844,32 @@
     AssertReturn(pv && cb <= sizeof(uint32_t), fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00);
 
-    if (MATCH_SCSI_CONFIG(            uNumQueues))
+    if (MATCH_SCSI_CONFIG(uNumQueues))
         SCSI_CONFIG_ACCESSOR_READONLY(uNumQueues);
-    else if (MATCH_SCSI_CONFIG(       uSegMax))
+    else
+    if (MATCH_SCSI_CONFIG(uSegMax))
         SCSI_CONFIG_ACCESSOR_READONLY(uSegMax);
-    else if (MATCH_SCSI_CONFIG(       uMaxSectors))
+    else
+    if (MATCH_SCSI_CONFIG(uMaxSectors))
         SCSI_CONFIG_ACCESSOR_READONLY(uMaxSectors);
-    else if (MATCH_SCSI_CONFIG(       uCmdPerLun))
+    else
+    if (MATCH_SCSI_CONFIG(uCmdPerLun))
         SCSI_CONFIG_ACCESSOR_READONLY(uCmdPerLun);
-    else if (MATCH_SCSI_CONFIG(       uEventInfoSize))
+    else
+    if (MATCH_SCSI_CONFIG(uEventInfoSize))
         SCSI_CONFIG_ACCESSOR_READONLY(uEventInfoSize);
-    else if (MATCH_SCSI_CONFIG(       uSenseSize))
-        SCSI_CONFIG_ACCESSOR(         uSenseSize);
-    else if (MATCH_SCSI_CONFIG(       uCdbSize))
-        SCSI_CONFIG_ACCESSOR(         uCdbSize);
-    else if (MATCH_SCSI_CONFIG(       uMaxChannel))
+    else
+    if (MATCH_SCSI_CONFIG(uSenseSize))
+        SCSI_CONFIG_ACCESSOR(uSenseSize);
+    else
+    if (MATCH_SCSI_CONFIG(uCdbSize))
+        SCSI_CONFIG_ACCESSOR(uCdbSize);
+    else
+    if (MATCH_SCSI_CONFIG(uMaxChannel))
         SCSI_CONFIG_ACCESSOR_READONLY(uMaxChannel);
-    else if (MATCH_SCSI_CONFIG(       uMaxTarget))
+    else
+    if (MATCH_SCSI_CONFIG(uMaxTarget))
         SCSI_CONFIG_ACCESSOR_READONLY(uMaxTarget);
-    else if (MATCH_SCSI_CONFIG(       uMaxLun))
+    else
+    if (MATCH_SCSI_CONFIG(uMaxLun))
         SCSI_CONFIG_ACCESSOR_READONLY(uMaxLun);
     else
@@ -2143,10 +2135,10 @@
  * The VM is suspended at this point.
  */
-static DECLCALLBACK(void) virtioScsiR3Detach(PPDMDEVINS pDevIns, unsigned iTarget, uint32_t fFlags)
+static DECLCALLBACK(void) virtioScsiR3Detach(PPDMDEVINS pDevIns, unsigned uTarget, uint32_t fFlags)
 {
     PVIRTIOSCSI       pThis   = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
     PVIRTIOSCSICC     pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
-    AssertReturnVoid(iTarget < pThis->cTargets);
-    PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[iTarget];
+    AssertReturnVoid(uTarget < pThis->cTargets);
+    PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
 
     LogFunc((""));
@@ -2163,5 +2155,4 @@
     pTarget->pDrvMedia      = NULL;
     pTarget->pDrvMediaEx    = NULL;
-    pTarget->pMediaNotify   = NULL;
 }
 
@@ -2171,10 +2162,10 @@
  * This is called when we change block driver.
  */
-static DECLCALLBACK(int) virtioScsiR3Attach(PPDMDEVINS pDevIns, unsigned iTarget, uint32_t fFlags)
+static DECLCALLBACK(int) virtioScsiR3Attach(PPDMDEVINS pDevIns, unsigned uTarget, uint32_t fFlags)
 {
     PVIRTIOSCSI       pThis   = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
     PVIRTIOSCSICC     pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
-    AssertReturn(iTarget < pThis->cTargets, VERR_PDM_LUN_NOT_FOUND);
-    PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[iTarget];
+    AssertReturn(uTarget < pThis->cTargets, VERR_PDM_LUN_NOT_FOUND);
+    PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
 
     Assert(pTarget->pDevIns == pDevIns);
@@ -2183,13 +2174,11 @@
                     VERR_INVALID_PARAMETER);
 
-    /* the usual paranoia */
     AssertRelease(!pTarget->pDrvBase);
-    Assert(pTarget->iTarget == iTarget);
+    Assert(pTarget->uTarget == uTarget);
 
     /*
-     * Try attach the SCSI driver and get the interfaces,
-     * required as well as optional.
+     * Try attach the SCSI driver and get the interfaces, required as well as optional.
      */
-    int rc = PDMDevHlpDriverAttach(pDevIns, pTarget->iTarget, &pDevIns->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
+    int rc = PDMDevHlpDriverAttach(pDevIns, pTarget->uTarget, &pDevIns->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
     if (RT_SUCCESS(rc))
     {
@@ -2197,5 +2186,5 @@
         pTarget->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIA);
         AssertMsgReturn(VALID_PTR(pTarget->pDrvMedia),
-                        ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", iTarget),
+                        ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", uTarget),
                         VERR_PDM_MISSING_INTERFACE);
 
@@ -2203,15 +2192,11 @@
         pTarget->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIAEX);
         AssertMsgReturn(VALID_PTR(pTarget->pDrvMediaEx),
-                        ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", iTarget),
+                        ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", uTarget),
                         VERR_PDM_MISSING_INTERFACE);
 
         rc = pTarget->pDrvMediaEx->pfnIoReqAllocSizeSet(pTarget->pDrvMediaEx, sizeof(VIRTIOSCSIREQ));
         AssertMsgReturn(VALID_PTR(pTarget->pDrvMediaEx),
-                        ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", iTarget),
+                        ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", uTarget),
                         rc);
-/*
-            pTarget->pMediaNotify = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIANOTIFY);
-*/
-
     }
     else
@@ -2224,5 +2209,5 @@
         pTarget->pDrvMedia      = NULL;
         pTarget->pDrvMediaEx    = NULL;
-        pTarget->pMediaNotify   = NULL;
+        pThisCC->pMediaNotify   = NULL;
     }
     return rc;
@@ -2362,10 +2347,13 @@
     PVIRTIOSCSICC       pThisCC   = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
 
-    LogFunc(("LUN %d Ejected!\n", pTarget->iTarget));
+#if 0 /* need more info about how to use this event */
+    virtioScsiR3ReportMediaChange(pDevIns, pThis, pTarget->uTarget);
+#endif
+
     if (pThisCC->pMediaNotify)
     {
         int rc = VMR3ReqCallNoWait(PDMDevHlpGetVM(pDevIns), VMCPUID_ANY,
                                    (PFNRT)pThisCC->pMediaNotify->pfnEjected, 2,
-                                   pThisCC->pMediaNotify, pTarget->iTarget);
+                                   pThisCC->pMediaNotify, pTarget->uTarget);
         AssertRC(rc);
     }
@@ -2412,4 +2400,5 @@
     RTMemFree(pThisCC->paTargetInstances);
     pThisCC->paTargetInstances = NULL;
+    pThisCC->pMediaNotify = NULL;
 
     for (unsigned qIdx = 0; qIdx < VIRTIOSCSI_QUEUE_CNT; qIdx++)
@@ -2540,5 +2529,4 @@
      * Initialize per device instances (targets).
      */
-
     Log2Func(("Probing %d targets ...\n", pThis->cTargets));
 
@@ -2547,14 +2535,14 @@
         return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to allocate memory for target states"));
 
-    for (uint32_t iTarget = 0; iTarget < pThis->cTargets; iTarget++)
-    {
-        PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[iTarget];
-
-        if (RTStrAPrintf(&pTarget->pszTargetName, "VSCSI%u", iTarget) < 0)
+    for (uint32_t uTarget = 0; uTarget < pThis->cTargets; uTarget++)
+    {
+        PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
+
+        if (RTStrAPrintf(&pTarget->pszTargetName, "VSCSI%u", uTarget) < 0)
             AssertLogRelFailedReturn(VERR_NO_MEMORY);
 
         /* Initialize static parts of the device. */
         pTarget->pDevIns = pDevIns;
-        pTarget->iTarget = iTarget;
+        pTarget->uTarget = uTarget;
         pTarget->led.u32Magic = PDMLED_MAGIC;
 
@@ -2572,5 +2560,4 @@
         pTarget->IMediaExPort.pfnIoReqQueryDiscardRanges = NULL;
 
-
         pTarget->IBase.pfnQueryInterface                 = virtioScsiR3TargetQueryInterface;
         pTarget->ILed.pfnQueryStatusLed                  = virtioScsiR3TargetQueryStatusLed;
@@ -2579,6 +2566,6 @@
         LogFunc(("Attaching LUN: %s\n", pTarget->pszTargetName));
 
-        AssertReturn(iTarget < pThis->cTargets, VERR_PDM_NO_SUCH_LUN);
-        rc = PDMDevHlpDriverAttach(pDevIns, iTarget, &pTarget->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
+        AssertReturn(uTarget < pThis->cTargets, VERR_PDM_NO_SUCH_LUN);
+        rc = PDMDevHlpDriverAttach(pDevIns, uTarget, &pTarget->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
         if (RT_SUCCESS(rc))
         {
@@ -2587,19 +2574,16 @@
             pTarget->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIA);
             AssertMsgReturn(VALID_PTR(pTarget->pDrvMedia),
-                            ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", iTarget),
+                            ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", uTarget),
                             VERR_PDM_MISSING_INTERFACE);
             /* Get the extended media interface. */
             pTarget->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIAEX);
             AssertMsgReturn(VALID_PTR(pTarget->pDrvMediaEx),
-                            ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", iTarget),
+                            ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", uTarget),
                             VERR_PDM_MISSING_INTERFACE);
 
             rc = pTarget->pDrvMediaEx->pfnIoReqAllocSizeSet(pTarget->pDrvMediaEx, sizeof(VIRTIOSCSIREQ));
             AssertMsgReturn(VALID_PTR(pTarget->pDrvMediaEx),
-                            ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", iTarget),
+                            ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", uTarget),
                             rc);
-/*
-            pTarget->pMediaNotify = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIANOTIFY);
-*/
         }
         else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
@@ -2625,4 +2609,5 @@
     if (RT_FAILURE(rc) && rc != VERR_PDM_NO_ATTACHED_DRIVER)
         return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the status LUN"));
+    pThisCC->pMediaNotify = PDMIBASE_QUERY_INTERFACE(pUpBase, PDMIMEDIANOTIFY);
 
 
