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