1 | /* $Id: VSCSIDevice.cpp 38680 2011-09-08 07:52:08Z 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 | */
|
---|
39 | DECLINLINE(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 | */
|
---|
53 | static 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 | *prcReq = vscsiReqSenseOkSet(&pVScsiDevice->VScsiSense, pVScsiReq);
|
---|
105 | break;
|
---|
106 | }
|
---|
107 | case SCSI_REQUEST_SENSE:
|
---|
108 | {
|
---|
109 | /* Descriptor format sense data is not supported and results in an error. */
|
---|
110 | if ((pVScsiReq->pbCDB[1] & 0x1) != 0)
|
---|
111 | *prcReq = vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
|
---|
112 | else
|
---|
113 | *prcReq = vscsiReqSenseCmd(&pVScsiDevice->VScsiSense, pVScsiReq);
|
---|
114 | }
|
---|
115 | default:
|
---|
116 | fProcessed = false;
|
---|
117 | }
|
---|
118 |
|
---|
119 | return fProcessed;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | void vscsiDeviceReqComplete(PVSCSIDEVICEINT pVScsiDevice, PVSCSIREQINT pVScsiReq,
|
---|
124 | int rcScsiCode, bool fRedoPossible, int rcReq)
|
---|
125 | {
|
---|
126 | pVScsiDevice->pfnVScsiReqCompleted(pVScsiDevice, pVScsiDevice->pvVScsiDeviceUser,
|
---|
127 | pVScsiReq->pvVScsiReqUser, rcScsiCode, fRedoPossible,
|
---|
128 | rcReq);
|
---|
129 |
|
---|
130 | RTMemCacheFree(pVScsiDevice->hCacheReq, pVScsiReq);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | VBOXDDU_DECL(int) VSCSIDeviceCreate(PVSCSIDEVICE phVScsiDevice,
|
---|
135 | PFNVSCSIREQCOMPLETED pfnVScsiReqCompleted,
|
---|
136 | void *pvVScsiDeviceUser)
|
---|
137 | {
|
---|
138 | int rc = VINF_SUCCESS;
|
---|
139 | PVSCSIDEVICEINT pVScsiDevice = NULL;
|
---|
140 |
|
---|
141 | AssertPtrReturn(phVScsiDevice, VERR_INVALID_POINTER);
|
---|
142 | AssertPtrReturn(pfnVScsiReqCompleted, VERR_INVALID_POINTER);
|
---|
143 |
|
---|
144 | pVScsiDevice = (PVSCSIDEVICEINT)RTMemAllocZ(sizeof(VSCSIDEVICEINT));
|
---|
145 | if (!pVScsiDevice)
|
---|
146 | return VERR_NO_MEMORY;
|
---|
147 |
|
---|
148 | pVScsiDevice->pfnVScsiReqCompleted = pfnVScsiReqCompleted;
|
---|
149 | pVScsiDevice->pvVScsiDeviceUser = pvVScsiDeviceUser;
|
---|
150 | pVScsiDevice->cLunsAttached = 0;
|
---|
151 | pVScsiDevice->cLunsMax = 0;
|
---|
152 | pVScsiDevice->papVScsiLun = NULL;
|
---|
153 | vscsiSenseInit(&pVScsiDevice->VScsiSense);
|
---|
154 |
|
---|
155 | rc = RTMemCacheCreate(&pVScsiDevice->hCacheReq, sizeof(VSCSIREQINT), 0, UINT32_MAX,
|
---|
156 | NULL, NULL, NULL, 0);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | {
|
---|
159 | *phVScsiDevice = pVScsiDevice;
|
---|
160 | LogFlow(("%s: hVScsiDevice=%#p -> VINF_SUCCESS\n", __FUNCTION__, pVScsiDevice));
|
---|
161 | return VINF_SUCCESS;
|
---|
162 | }
|
---|
163 |
|
---|
164 | RTMemFree(pVScsiDevice);
|
---|
165 |
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | VBOXDDU_DECL(int) VSCSIDeviceDestroy(VSCSIDEVICE hVScsiDevice)
|
---|
171 | {
|
---|
172 | AssertPtrReturn(hVScsiDevice, VERR_INVALID_HANDLE);
|
---|
173 |
|
---|
174 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
175 |
|
---|
176 | if (pVScsiDevice->cLunsAttached > 0)
|
---|
177 | return VERR_VSCSI_LUN_ATTACHED_TO_DEVICE;
|
---|
178 |
|
---|
179 | if (pVScsiDevice->papVScsiLun)
|
---|
180 | RTMemFree(pVScsiDevice->papVScsiLun);
|
---|
181 |
|
---|
182 | RTMemCacheDestroy(pVScsiDevice->hCacheReq);
|
---|
183 | RTMemFree(pVScsiDevice);
|
---|
184 |
|
---|
185 | return VINF_SUCCESS;;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | VBOXDDU_DECL(int) VSCSIDeviceLunAttach(VSCSIDEVICE hVScsiDevice, VSCSILUN hVScsiLun, uint32_t iLun)
|
---|
190 | {
|
---|
191 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
192 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
193 | int rc = VINF_SUCCESS;
|
---|
194 |
|
---|
195 | /* Parameter checks */
|
---|
196 | AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
|
---|
197 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
198 | AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
|
---|
199 | AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
|
---|
200 |
|
---|
201 | if (iLun >= pVScsiDevice->cLunsMax)
|
---|
202 | {
|
---|
203 | PPVSCSILUNINT papLunOld = pVScsiDevice->papVScsiLun;
|
---|
204 |
|
---|
205 | pVScsiDevice->papVScsiLun = (PPVSCSILUNINT)RTMemAllocZ((iLun + 1) * sizeof(PVSCSILUNINT));
|
---|
206 | if (pVScsiDevice->papVScsiLun)
|
---|
207 | {
|
---|
208 | for (uint32_t i = 0; i < pVScsiDevice->cLunsMax; i++)
|
---|
209 | pVScsiDevice->papVScsiLun[i] = papLunOld[i];
|
---|
210 |
|
---|
211 | if (papLunOld)
|
---|
212 | RTMemFree(papLunOld);
|
---|
213 |
|
---|
214 | pVScsiDevice->cLunsMax = iLun + 1;
|
---|
215 | }
|
---|
216 | else
|
---|
217 | rc = VERR_NO_MEMORY;
|
---|
218 | }
|
---|
219 |
|
---|
220 | if (RT_SUCCESS(rc))
|
---|
221 | {
|
---|
222 | pVScsiLun->pVScsiDevice = pVScsiDevice;
|
---|
223 | pVScsiDevice->papVScsiLun[iLun] = pVScsiLun;
|
---|
224 | pVScsiDevice->cLunsAttached++;
|
---|
225 | }
|
---|
226 |
|
---|
227 | return rc;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | VBOXDDU_DECL(int) VSCSIDeviceLunDetach(VSCSIDEVICE hVScsiDevice, uint32_t iLun,
|
---|
232 | PVSCSILUN phVScsiLun)
|
---|
233 | {
|
---|
234 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
235 |
|
---|
236 | /* Parameter checks */
|
---|
237 | AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
|
---|
238 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
239 | AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
|
---|
240 | AssertReturn(iLun < pVScsiDevice->cLunsMax, VERR_VSCSI_LUN_NOT_ATTACHED);
|
---|
241 | AssertPtrReturn(pVScsiDevice->papVScsiLun[iLun], VERR_VSCSI_LUN_NOT_ATTACHED);
|
---|
242 |
|
---|
243 | PVSCSILUNINT pVScsiLun = pVScsiDevice->papVScsiLun[iLun];
|
---|
244 |
|
---|
245 | pVScsiLun->pVScsiDevice = NULL;
|
---|
246 | *phVScsiLun = pVScsiLun;
|
---|
247 | pVScsiDevice->papVScsiLun[iLun] = NULL;
|
---|
248 | pVScsiDevice->cLunsAttached--;
|
---|
249 |
|
---|
250 | return VINF_SUCCESS;
|
---|
251 | }
|
---|
252 |
|
---|
253 |
|
---|
254 | VBOXDDU_DECL(int) VSCSIDeviceLunGet(VSCSIDEVICE hVScsiDevice, uint32_t iLun,
|
---|
255 | PVSCSILUN phVScsiLun)
|
---|
256 | {
|
---|
257 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
258 |
|
---|
259 | /* Parameter checks */
|
---|
260 | AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
|
---|
261 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
262 | AssertReturn(iLun < VSCSI_DEVICE_LUN_MAX, VERR_VSCSI_LUN_INVALID);
|
---|
263 | AssertReturn(iLun < pVScsiDevice->cLunsMax, VERR_VSCSI_LUN_NOT_ATTACHED);
|
---|
264 | AssertPtrReturn(pVScsiDevice->papVScsiLun[iLun], VERR_VSCSI_LUN_NOT_ATTACHED);
|
---|
265 |
|
---|
266 | *phVScsiLun = pVScsiDevice->papVScsiLun[iLun];
|
---|
267 |
|
---|
268 | return VINF_SUCCESS;
|
---|
269 | }
|
---|
270 |
|
---|
271 |
|
---|
272 | VBOXDDU_DECL(int) VSCSIDeviceReqEnqueue(VSCSIDEVICE hVScsiDevice, VSCSIREQ hVScsiReq)
|
---|
273 | {
|
---|
274 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
275 | PVSCSIREQINT pVScsiReq = (PVSCSIREQINT)hVScsiReq;
|
---|
276 | int rc = VINF_SUCCESS;
|
---|
277 |
|
---|
278 | /* Parameter checks */
|
---|
279 | AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
|
---|
280 | AssertPtrReturn(pVScsiReq, VERR_INVALID_HANDLE);
|
---|
281 |
|
---|
282 | /* Check if this request can be handled by us */
|
---|
283 | int rcReq;
|
---|
284 | bool fProcessed = vscsiDeviceReqProcess(pVScsiDevice, pVScsiReq, &rcReq);
|
---|
285 | if (!fProcessed)
|
---|
286 | {
|
---|
287 | /* Pass to the LUN driver */
|
---|
288 | if (vscsiDeviceLunIsPresent(pVScsiDevice, pVScsiReq->iLun))
|
---|
289 | {
|
---|
290 | PVSCSILUNINT pVScsiLun = pVScsiDevice->papVScsiLun[pVScsiReq->iLun];
|
---|
291 | rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunReqProcess(pVScsiLun, pVScsiReq);
|
---|
292 | }
|
---|
293 | else
|
---|
294 | {
|
---|
295 | /* LUN not present, report error. */
|
---|
296 | vscsiReqSenseErrorSet(&pVScsiDevice->VScsiSense, pVScsiReq,
|
---|
297 | SCSI_SENSE_ILLEGAL_REQUEST,
|
---|
298 | SCSI_ASC_LOGICAL_UNIT_DOES_NOT_RESPOND_TO_SELECTION,
|
---|
299 | 0x00);
|
---|
300 |
|
---|
301 | vscsiDeviceReqComplete(pVScsiDevice, pVScsiReq,
|
---|
302 | SCSI_STATUS_CHECK_CONDITION, false, VINF_SUCCESS);
|
---|
303 | }
|
---|
304 | }
|
---|
305 | else
|
---|
306 | vscsiDeviceReqComplete(pVScsiDevice, pVScsiReq,
|
---|
307 | rcReq, false, VINF_SUCCESS);
|
---|
308 |
|
---|
309 | return rc;
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | VBOXDDU_DECL(int) VSCSIDeviceReqCreate(VSCSIDEVICE hVScsiDevice, PVSCSIREQ phVScsiReq,
|
---|
314 | uint32_t iLun, uint8_t *pbCDB, size_t cbCDB,
|
---|
315 | size_t cbSGList, unsigned cSGListEntries,
|
---|
316 | PCRTSGSEG paSGList, uint8_t *pbSense,
|
---|
317 | size_t cbSense, void *pvVScsiReqUser)
|
---|
318 | {
|
---|
319 | PVSCSIDEVICEINT pVScsiDevice = (PVSCSIDEVICEINT)hVScsiDevice;
|
---|
320 | PVSCSIREQINT pVScsiReq = NULL;
|
---|
321 |
|
---|
322 | /* Parameter checks */
|
---|
323 | AssertPtrReturn(pVScsiDevice, VERR_INVALID_HANDLE);
|
---|
324 | AssertPtrReturn(phVScsiReq, VERR_INVALID_POINTER);
|
---|
325 | AssertPtrReturn(pbCDB, VERR_INVALID_PARAMETER);
|
---|
326 | AssertReturn(cbCDB > 0, VERR_INVALID_PARAMETER);
|
---|
327 |
|
---|
328 | pVScsiReq = (PVSCSIREQINT)RTMemCacheAlloc(pVScsiDevice->hCacheReq);
|
---|
329 | if (!pVScsiReq)
|
---|
330 | return VERR_NO_MEMORY;
|
---|
331 |
|
---|
332 | pVScsiReq->iLun = iLun;
|
---|
333 | pVScsiReq->pbCDB = pbCDB;
|
---|
334 | pVScsiReq->cbCDB = cbCDB;
|
---|
335 | pVScsiReq->pbSense = pbSense;
|
---|
336 | pVScsiReq->cbSense = cbSense;
|
---|
337 | pVScsiReq->pvVScsiReqUser = pvVScsiReqUser;
|
---|
338 |
|
---|
339 | if (cSGListEntries)
|
---|
340 | RTSgBufInit(&pVScsiReq->SgBuf, paSGList, cSGListEntries);
|
---|
341 |
|
---|
342 | *phVScsiReq = pVScsiReq;
|
---|
343 |
|
---|
344 | return VINF_SUCCESS;
|
---|
345 | }
|
---|
346 |
|
---|