VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VSCSI/VSCSIDevice.cpp@ 43659

Last change on this file since 43659 was 43640, checked in by vboxsync, 12 years ago

VSCSI: Added basic media change support.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: VSCSIDevice.cpp 43640 2012-10-15 12:39:52Z vboxsync $ */
2/** @file
3 * Virtual SCSI driver: Device handling
4 */
5
6/*
7 * Copyright (C) 2006-2011 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* Header Files *
19*******************************************************************************/
20#define LOG_GROUP LOG_GROUP_VSCSI
21#include <VBox/log.h>
22#include <VBox/err.h>
23#include <VBox/types.h>
24#include <VBox/vscsi.h>
25#include <iprt/assert.h>
26#include <iprt/mem.h>
27#include <iprt/string.h>
28
29#include "VSCSIInternal.h"
30
31/**
32 * Checks if a specific LUN exists fir the SCSI device
33 *
34 * @returns true if the LUN is present
35 * false otherwise
36 * @param pVScsiDevice The SCSI device instance.
37 * @param iLun The LUN to check for.
38 */
39DECLINLINE(bool) vscsiDeviceLunIsPresent(PVSCSIDEVICEINT pVScsiDevice, uint32_t iLun)
40{
41 return ( iLun < pVScsiDevice->cLunsMax
42 && pVScsiDevice->papVScsiLun[iLun] != NULL);
43}
44
45/**
46 * Process a request common for all device types.
47 *
48 * @returns Flag whether we could handle the request.
49 * @param pVScsiDevice The virtual SCSI device instance.
50 * @param pVScsiReq The SCSi request.
51 * @param prcReq The final return value if the request was handled.
52 */
53static bool vscsiDeviceReqProcess(PVSCSIDEVICEINT pVScsiDevice, PVSCSIREQINT pVScsiReq,
54 int *prcReq)
55{
56 bool fProcessed = true;
57
58 switch (pVScsiReq->pbCDB[0])
59 {
60 case SCSI_INQUIRY:
61 {
62 if (!vscsiDeviceLunIsPresent(pVScsiDevice, pVScsiReq->iLun))
63 {
64 size_t cbData;
65 SCSIINQUIRYDATA ScsiInquiryReply;
66
67 memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
68 ScsiInquiryReply.cbAdditional = 31;
69 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
70 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
71 cbData = RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, (uint8_t *)&ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
72 *prcReq = vscsiReqSenseOkSet(&pVScsiDevice->VScsiSense, pVScsiReq);
73 }
74 else
75 fProcessed = false; /* Let the LUN process the request because it will provide LUN specific data */
76
77 break;
78 }
79 case SCSI_REPORT_LUNS:
80 {
81 /*
82 * If allocation length is less than 16 bytes SPC compliant devices have
83 * to return an error.
84 */
85 if (vscsiBE2HU32(&pVScsiReq->pbCDB[6]) < 16)
86 *prcReq = vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
87 else
88 {
89 size_t cbData;
90 uint8_t aReply[16]; /* We report only one LUN. */
91
92 memset(aReply, 0, sizeof(aReply));
93 vscsiH2BEU32(&aReply[0], 8); /* List length starts at position 0. */
94 cbData = RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
95 if (cbData < 16)
96 *prcReq = vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
97 else
98 *prcReq = vscsiReqSenseOkSet(&pVScsiDevice->VScsiSense, pVScsiReq);
99 }
100 break;
101 }
102 case SCSI_TEST_UNIT_READY:
103 {
104 if (pVScsiDevice->papVScsiLun[pVScsiReq->iLun]->fReady)
105 *prcReq = vscsiReqSenseOkSet(&pVScsiDevice->VScsiSense, pVScsiReq);
106 else
107 fProcessed = false; /* The LUN will provide details. */
108 break;
109 }
110 case SCSI_REQUEST_SENSE:
111 {
112 /* Descriptor format sense data is not supported and results in an error. */
113 if ((pVScsiReq->pbCDB[1] & 0x1) != 0)
114 *prcReq = vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
115 else
116 *prcReq = vscsiReqSenseCmd(&pVScsiDevice->VScsiSense, pVScsiReq);
117 }
118 default:
119 fProcessed = false;
120 }
121
122 return fProcessed;
123}
124
125
126void vscsiDeviceReqComplete(PVSCSIDEVICEINT pVScsiDevice, PVSCSIREQINT pVScsiReq,
127 int rcScsiCode, bool fRedoPossible, int rcReq)
128{
129 pVScsiDevice->pfnVScsiReqCompleted(pVScsiDevice, pVScsiDevice->pvVScsiDeviceUser,
130 pVScsiReq->pvVScsiReqUser, rcScsiCode, fRedoPossible,
131 rcReq);
132
133 RTMemCacheFree(pVScsiDevice->hCacheReq, pVScsiReq);
134}
135
136
137VBOXDDU_DECL(int) VSCSIDeviceCreate(PVSCSIDEVICE phVScsiDevice,
138 PFNVSCSIREQCOMPLETED pfnVScsiReqCompleted,
139 void *pvVScsiDeviceUser)
140{
141 int rc = VINF_SUCCESS;
142 PVSCSIDEVICEINT pVScsiDevice = NULL;
143
144 AssertPtrReturn(phVScsiDevice, VERR_INVALID_POINTER);
145 AssertPtrReturn(pfnVScsiReqCompleted, VERR_INVALID_POINTER);
146
147 pVScsiDevice = (PVSCSIDEVICEINT)RTMemAllocZ(sizeof(VSCSIDEVICEINT));
148 if (!pVScsiDevice)
149 return VERR_NO_MEMORY;
150
151 pVScsiDevice->pfnVScsiReqCompleted = pfnVScsiReqCompleted;
152 pVScsiDevice->pvVScsiDeviceUser = pvVScsiDeviceUser;
153 pVScsiDevice->cLunsAttached = 0;
154 pVScsiDevice->cLunsMax = 0;
155 pVScsiDevice->papVScsiLun = NULL;
156 vscsiSenseInit(&pVScsiDevice->VScsiSense);
157
158 rc = RTMemCacheCreate(&pVScsiDevice->hCacheReq, sizeof(VSCSIREQINT), 0, UINT32_MAX,
159 NULL, NULL, NULL, 0);
160 if (RT_SUCCESS(rc))
161 {
162 *phVScsiDevice = pVScsiDevice;
163 LogFlow(("%s: hVScsiDevice=%#p -> VINF_SUCCESS\n", __FUNCTION__, pVScsiDevice));
164 return VINF_SUCCESS;
165 }
166
167 RTMemFree(pVScsiDevice);
168
169 return rc;
170}
171
172
173VBOXDDU_DECL(int) VSCSIDeviceDestroy(VSCSIDEVICE hVScsiDevice)
174{
175 AssertPtrReturn(hVScsiDevice, VERR_INVALID_HANDLE);
176
177 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
178
179 if (pVScsiDevice->cLunsAttached > 0)
180 return VERR_VSCSI_LUN_ATTACHED_TO_DEVICE;
181
182 if (pVScsiDevice->papVScsiLun)
183 RTMemFree(pVScsiDevice->papVScsiLun);
184
185 RTMemCacheDestroy(pVScsiDevice->hCacheReq);
186 RTMemFree(pVScsiDevice);
187
188 return VINF_SUCCESS;;
189}
190
191
192VBOXDDU_DECL(int) VSCSIDeviceLunAttach(VSCSIDEVICE hVScsiDevice, VSCSILUN hVScsiLun, uint32_t iLun)
193{
194 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
195 PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
196 int rc = VINF_SUCCESS;
197
198 /* Parameter checks */
199 AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
200 AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
201 AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
202 AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
203
204 if (iLun >= pVScsiDevice->cLunsMax)
205 {
206 PPVSCSILUNINT papLunOld = pVScsiDevice->papVScsiLun;
207
208 pVScsiDevice->papVScsiLun = (PPVSCSILUNINT)RTMemAllocZ((iLun + 1) * sizeof(PVSCSILUNINT));
209 if (pVScsiDevice->papVScsiLun)
210 {
211 for (uint32_t i = 0; i < pVScsiDevice->cLunsMax; i++)
212 pVScsiDevice->papVScsiLun[i] = papLunOld[i];
213
214 if (papLunOld)
215 RTMemFree(papLunOld);
216
217 pVScsiDevice->cLunsMax = iLun + 1;
218 }
219 else
220 rc = VERR_NO_MEMORY;
221 }
222
223 if (RT_SUCCESS(rc))
224 {
225 pVScsiLun->pVScsiDevice = pVScsiDevice;
226 pVScsiDevice->papVScsiLun[iLun] = pVScsiLun;
227 pVScsiDevice->cLunsAttached++;
228 }
229
230 return rc;
231}
232
233
234VBOXDDU_DECL(int) VSCSIDeviceLunDetach(VSCSIDEVICE hVScsiDevice, uint32_t iLun,
235 PVSCSILUN phVScsiLun)
236{
237 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
238
239 /* Parameter checks */
240 AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
241 AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
242 AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
243 AssertReturn(iLun < pVScsiDevice->cLunsMax, VERR_VSCSI_LUN_NOT_ATTACHED);
244 AssertPtrReturn(pVScsiDevice->papVScsiLun[iLun], VERR_VSCSI_LUN_NOT_ATTACHED);
245
246 PVSCSILUNINT pVScsiLun = pVScsiDevice->papVScsiLun[iLun];
247
248 pVScsiLun->pVScsiDevice = NULL;
249 *phVScsiLun = pVScsiLun;
250 pVScsiDevice->papVScsiLun[iLun] = NULL;
251 pVScsiDevice->cLunsAttached--;
252
253 return VINF_SUCCESS;
254}
255
256
257VBOXDDU_DECL(int) VSCSIDeviceLunGet(VSCSIDEVICE hVScsiDevice, uint32_t iLun,
258 PVSCSILUN phVScsiLun)
259{
260 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
261
262 /* Parameter checks */
263 AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
264 AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
265 AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
266 AssertReturn(iLun < pVScsiDevice->cLunsMax, VERR_VSCSI_LUN_NOT_ATTACHED);
267 AssertPtrReturn(pVScsiDevice->papVScsiLun[iLun], VERR_VSCSI_LUN_NOT_ATTACHED);
268
269 *phVScsiLun = pVScsiDevice->papVScsiLun[iLun];
270
271 return VINF_SUCCESS;
272}
273
274
275VBOXDDU_DECL(int) VSCSIDeviceReqEnqueue(VSCSIDEVICE hVScsiDevice, VSCSIREQ hVScsiReq)
276{
277 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
278 PVSCSIREQINT pVScsiReq = (PVSCSIREQINT)hVScsiReq;
279 int rc = VINF_SUCCESS;
280
281 /* Parameter checks */
282 AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
283 AssertPtrReturn(pVScsiReq, VERR_INVALID_HANDLE);
284
285 /* Check if this request can be handled by us */
286 int rcReq;
287 bool fProcessed = vscsiDeviceReqProcess(pVScsiDevice, pVScsiReq, &rcReq);
288 if (!fProcessed)
289 {
290 /* Pass to the LUN driver */
291 if (vscsiDeviceLunIsPresent(pVScsiDevice, pVScsiReq->iLun))
292 {
293 PVSCSILUNINT pVScsiLun = pVScsiDevice->papVScsiLun[pVScsiReq->iLun];
294 rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunReqProcess(pVScsiLun, pVScsiReq);
295 }
296 else
297 {
298 /* LUN not present, report error. */
299 vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq,
300 SCSI_SENSE_ILLEGAL_REQUEST,
301 SCSI_ASC_LOGICAL_UNIT_DOES_NOT_RESPOND_TO_SELECTION,
302 0x00);
303
304 vscsiDeviceReqComplete(pVScsiDevice, pVScsiReq,
305 SCSI_STATUS_CHECK_CONDITION, false, VINF_SUCCESS);
306 }
307 }
308 else
309 vscsiDeviceReqComplete(pVScsiDevice, pVScsiReq,
310 rcReq, false, VINF_SUCCESS);
311
312 return rc;
313}
314
315
316VBOXDDU_DECL(int) VSCSIDeviceReqCreate(VSCSIDEVICE hVScsiDevice, PVSCSIREQ phVScsiReq,
317 uint32_t iLun, uint8_t *pbCDB, size_t cbCDB,
318 size_t cbSGList, unsigned cSGListEntries,
319 PCRTSGSEG paSGList, uint8_t *pbSense,
320 size_t cbSense, void *pvVScsiReqUser)
321{
322 PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
323 PVSCSIREQINT pVScsiReq = NULL;
324
325 /* Parameter checks */
326 AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
327 AssertPtrReturn(phVScsiReq, VERR_INVALID_POINTER);
328 AssertPtrReturn(pbCDB, VERR_INVALID_PARAMETER);
329 AssertReturn(cbCDB > 0, VERR_INVALID_PARAMETER);
330
331 pVScsiReq = (PVSCSIREQINT)RTMemCacheAlloc(pVScsiDevice->hCacheReq);
332 if (!pVScsiReq)
333 return VERR_NO_MEMORY;
334
335 pVScsiReq->iLun = iLun;
336 pVScsiReq->pbCDB = pbCDB;
337 pVScsiReq->cbCDB = cbCDB;
338 pVScsiReq->pbSense = pbSense;
339 pVScsiReq->cbSense = cbSense;
340 pVScsiReq->pvVScsiReqUser = pvVScsiReqUser;
341
342 if (cSGListEntries)
343 RTSgBufInit(&pVScsiReq->SgBuf, paSGList, cSGListEntries);
344
345 *phVScsiReq = pVScsiReq;
346
347 return VINF_SUCCESS;
348}
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