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