VirtualBox

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

Last change on this file since 28800 was 28800, checked in by vboxsync, 15 years ago

Automated rebranding to Oracle copyright/license strings via filemuncher

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