1 | /* $Id: VSCSILun.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: LUN handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | #define LOG_GROUP LOG_GROUP_VSCSI
|
---|
18 | #include <VBox/log.h>
|
---|
19 | #include <VBox/err.h>
|
---|
20 | #include <VBox/types.h>
|
---|
21 | #include <VBox/vscsi.h>
|
---|
22 | #include <iprt/assert.h>
|
---|
23 | #include <iprt/mem.h>
|
---|
24 |
|
---|
25 | #include "VSCSIInternal.h"
|
---|
26 |
|
---|
27 | /** SBC descriptor */
|
---|
28 | extern VSCSILUNDESC g_VScsiLunTypeSbc;
|
---|
29 | /** MMC descriptor */
|
---|
30 | extern VSCSILUNDESC g_VScsiLunTypeMmc;
|
---|
31 | /** SSC descriptor */
|
---|
32 | extern VSCSILUNDESC g_VScsiLunTypeSsc;
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Array of supported SCSI LUN types.
|
---|
36 | */
|
---|
37 | static PVSCSILUNDESC g_aVScsiLunTypesSupported[] =
|
---|
38 | {
|
---|
39 | &g_VScsiLunTypeSbc,
|
---|
40 | &g_VScsiLunTypeMmc,
|
---|
41 | #ifdef VBOX_WITH_VSCSI_SSC
|
---|
42 | &g_VScsiLunTypeSsc,
|
---|
43 | #endif
|
---|
44 | };
|
---|
45 |
|
---|
46 | VBOXDDU_DECL(int) VSCSILunCreate(PVSCSILUN phVScsiLun, VSCSILUNTYPE enmLunType,
|
---|
47 | PVSCSILUNIOCALLBACKS pVScsiLunIoCallbacks,
|
---|
48 | void *pvVScsiLunUser)
|
---|
49 | {
|
---|
50 | PVSCSILUNINT pVScsiLun = NULL;
|
---|
51 | PVSCSILUNDESC pVScsiLunDesc = NULL;
|
---|
52 |
|
---|
53 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
54 | AssertReturn( enmLunType > VSCSILUNTYPE_INVALID
|
---|
55 | && enmLunType < VSCSILUNTYPE_LAST, VERR_INVALID_PARAMETER);
|
---|
56 | AssertPtrReturn(pVScsiLunIoCallbacks, VERR_INVALID_PARAMETER);
|
---|
57 |
|
---|
58 | for (unsigned idxLunType = 0; idxLunType < RT_ELEMENTS(g_aVScsiLunTypesSupported); idxLunType++)
|
---|
59 | {
|
---|
60 | if (g_aVScsiLunTypesSupported[idxLunType]->enmLunType == enmLunType)
|
---|
61 | {
|
---|
62 | pVScsiLunDesc = g_aVScsiLunTypesSupported[idxLunType];
|
---|
63 | break;
|
---|
64 | }
|
---|
65 | }
|
---|
66 |
|
---|
67 | if (!pVScsiLunDesc)
|
---|
68 | return VERR_VSCSI_LUN_TYPE_NOT_SUPPORTED;
|
---|
69 |
|
---|
70 | pVScsiLun = (PVSCSILUNINT)RTMemAllocZ(pVScsiLunDesc->cbLun);
|
---|
71 | if (!pVScsiLun)
|
---|
72 | return VERR_NO_MEMORY;
|
---|
73 |
|
---|
74 | pVScsiLun->pVScsiDevice = NULL;
|
---|
75 | pVScsiLun->pvVScsiLunUser = pvVScsiLunUser;
|
---|
76 | pVScsiLun->pVScsiLunIoCallbacks = pVScsiLunIoCallbacks;
|
---|
77 | pVScsiLun->pVScsiLunDesc = pVScsiLunDesc;
|
---|
78 |
|
---|
79 | int rc = vscsiIoReqInit(pVScsiLun);
|
---|
80 | if (RT_SUCCESS(rc))
|
---|
81 | {
|
---|
82 | rc = vscsiLunGetFeatureFlags(pVScsiLun, &pVScsiLun->fFeatures);
|
---|
83 | if (RT_SUCCESS(rc))
|
---|
84 | {
|
---|
85 | rc = pVScsiLunDesc->pfnVScsiLunInit(pVScsiLun);
|
---|
86 | if (RT_SUCCESS(rc))
|
---|
87 | {
|
---|
88 | *phVScsiLun = pVScsiLun;
|
---|
89 | return VINF_SUCCESS;
|
---|
90 | }
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | RTMemFree(pVScsiLun);
|
---|
95 |
|
---|
96 | return rc;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Destroy virtual SCSI LUN.
|
---|
101 | *
|
---|
102 | * @returns VBox status code.
|
---|
103 | * @param hVScsiLun The virtual SCSI LUN handle to destroy.
|
---|
104 | */
|
---|
105 | VBOXDDU_DECL(int) VSCSILunDestroy(VSCSILUN hVScsiLun)
|
---|
106 | {
|
---|
107 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
108 |
|
---|
109 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
110 | AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
|
---|
111 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
112 |
|
---|
113 | int rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunDestroy(pVScsiLun);
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | return rc;
|
---|
116 |
|
---|
117 | /* Make LUN invalid */
|
---|
118 | pVScsiLun->pvVScsiLunUser = NULL;
|
---|
119 | pVScsiLun->pVScsiLunIoCallbacks = NULL;
|
---|
120 | pVScsiLun->pVScsiLunDesc = NULL;
|
---|
121 |
|
---|
122 | RTMemFree(pVScsiLun);
|
---|
123 |
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Notify virtual SCSI LUN of media being mounted.
|
---|
129 | *
|
---|
130 | * @returns VBox status code.
|
---|
131 | * @param hVScsiLun The virtual SCSI LUN
|
---|
132 | * mounting the medium.
|
---|
133 | */
|
---|
134 | VBOXDDU_DECL(int) VSCSILunMountNotify(VSCSILUN hVScsiLun)
|
---|
135 | {
|
---|
136 | int rc = VINF_SUCCESS;
|
---|
137 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
138 |
|
---|
139 | LogFlowFunc(("hVScsiLun=%p\n", hVScsiLun));
|
---|
140 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
141 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
142 |
|
---|
143 | /* Mark the LUN as not ready so that LUN specific code can do its job. */
|
---|
144 | pVScsiLun->fReady = false;
|
---|
145 | pVScsiLun->fMediaPresent = true;
|
---|
146 | if (pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumInserted)
|
---|
147 | rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumInserted(pVScsiLun);
|
---|
148 |
|
---|
149 | return rc;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Notify virtual SCSI LUN of media being unmounted.
|
---|
154 | *
|
---|
155 | * @returns VBox status code.
|
---|
156 | * @param hVScsiLun The virtual SCSI LUN
|
---|
157 | * mounting the medium.
|
---|
158 | */
|
---|
159 | VBOXDDU_DECL(int) VSCSILunUnmountNotify(VSCSILUN hVScsiLun)
|
---|
160 | {
|
---|
161 | int rc = VINF_SUCCESS;
|
---|
162 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
163 |
|
---|
164 | LogFlowFunc(("hVScsiLun=%p\n", hVScsiLun));
|
---|
165 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
166 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
167 |
|
---|
168 | pVScsiLun->fReady = false;
|
---|
169 | pVScsiLun->fMediaPresent = false;
|
---|
170 | if (pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumRemoved)
|
---|
171 | rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunMediumRemoved(pVScsiLun);
|
---|
172 |
|
---|
173 | return rc;
|
---|
174 | }
|
---|