1 | /* $Id: DrvSCSIHost.cpp 54944 2015-03-25 15:23:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox storage drivers: Host SCSI access driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | /*******************************************************************************
|
---|
19 | * Header Files *
|
---|
20 | *******************************************************************************/
|
---|
21 | //#define DEBUG
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_SCSIHOST
|
---|
23 | #include <VBox/vmm/pdmdrv.h>
|
---|
24 | #include <VBox/vmm/pdmifs.h>
|
---|
25 | #include <VBox/vmm/pdmthread.h>
|
---|
26 | #include <VBox/scsi.h>
|
---|
27 | #include <iprt/assert.h>
|
---|
28 | #include <iprt/file.h>
|
---|
29 | #include <iprt/mem.h>
|
---|
30 | #include <iprt/req.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/uuid.h>
|
---|
33 |
|
---|
34 | #if defined(RT_OS_LINUX)
|
---|
35 | # include <limits.h>
|
---|
36 | # include <scsi/sg.h>
|
---|
37 | # include <sys/ioctl.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #include "VBoxDD.h"
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * SCSI driver instance data.
|
---|
44 | *
|
---|
45 | * @implements PDMISCSICONNECTOR
|
---|
46 | */
|
---|
47 | typedef struct DRVSCSIHOST
|
---|
48 | {
|
---|
49 | /** Pointer driver instance. */
|
---|
50 | PPDMDRVINS pDrvIns;
|
---|
51 |
|
---|
52 | /** Pointer to the SCSI port interface of the device above. */
|
---|
53 | PPDMISCSIPORT pDevScsiPort;
|
---|
54 | /** The SCSI connector interface. */
|
---|
55 | PDMISCSICONNECTOR ISCSIConnector;
|
---|
56 |
|
---|
57 | /** Path to the device file. */
|
---|
58 | char *pszDevicePath;
|
---|
59 | /** Handle to the device. */
|
---|
60 | RTFILE hDeviceFile;
|
---|
61 |
|
---|
62 | /** The dedicated I/O thread. */
|
---|
63 | PPDMTHREAD pAsyncIOThread;
|
---|
64 | /** Queue for passing the requests to the thread. */
|
---|
65 | RTREQQUEUE hQueueRequests;
|
---|
66 | } DRVSCSIHOST, *PDRVSCSIHOST;
|
---|
67 |
|
---|
68 | /** Converts a pointer to DRVSCSIHOST::ISCSIConnecotr to a PDRVSCSIHOST. */
|
---|
69 | #define PDMISCSICONNECTOR_2_DRVSCSIHOST(pInterface) ( (PDRVSCSIHOST)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSIHOST, ISCSIConnector)) )
|
---|
70 |
|
---|
71 | #ifdef DEBUG
|
---|
72 | /**
|
---|
73 | * Dumps a SCSI request structure for debugging purposes.
|
---|
74 | *
|
---|
75 | * @returns nothing.
|
---|
76 | * @param pRequest Pointer to the request to dump.
|
---|
77 | */
|
---|
78 | static void drvscsihostDumpScsiRequest(PPDMSCSIREQUEST pRequest)
|
---|
79 | {
|
---|
80 | Log(("Dump for pRequest=%#p Command: %s\n", pRequest, SCSICmdText(pRequest->pbCDB[0])));
|
---|
81 | Log(("cbCDB=%u\n", pRequest->cbCDB));
|
---|
82 | for (uint32_t i = 0; i < pRequest->cbCDB; i++)
|
---|
83 | Log(("pbCDB[%u]=%#x\n", i, pRequest->pbCDB[i]));
|
---|
84 | Log(("cbScatterGather=%u\n", pRequest->cbScatterGather));
|
---|
85 | Log(("cScatterGatherEntries=%u\n", pRequest->cScatterGatherEntries));
|
---|
86 | /* Print all scatter gather entries. */
|
---|
87 | for (uint32_t i = 0; i < pRequest->cScatterGatherEntries; i++)
|
---|
88 | {
|
---|
89 | Log(("ScatterGatherEntry[%u].cbSeg=%u\n", i, pRequest->paScatterGatherHead[i].cbSeg));
|
---|
90 | Log(("ScatterGatherEntry[%u].pvSeg=%#p\n", i, pRequest->paScatterGatherHead[i].pvSeg));
|
---|
91 | }
|
---|
92 | Log(("pvUser=%#p\n", pRequest->pvUser));
|
---|
93 | }
|
---|
94 | #endif
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Copy the content of a buffer to a scatter gather list
|
---|
98 | * copying only the amount of data which fits into the
|
---|
99 | * scatter gather list.
|
---|
100 | *
|
---|
101 | * @returns VBox status code.
|
---|
102 | * @param pRequest Pointer to the request which contains the S/G list entries.
|
---|
103 | * @param pvBuf Pointer to the buffer which should be copied.
|
---|
104 | * @param cbBuf Size of the buffer.
|
---|
105 | */
|
---|
106 | static int drvscsihostScatterGatherListCopyFromBuffer(PPDMSCSIREQUEST pRequest, void *pvBuf, size_t cbBuf)
|
---|
107 | {
|
---|
108 | unsigned cSGEntry = 0;
|
---|
109 | PRTSGSEG pSGEntry = &pRequest->paScatterGatherHead[cSGEntry];
|
---|
110 | uint8_t *pu8Buf = (uint8_t *)pvBuf;
|
---|
111 |
|
---|
112 | while (cSGEntry < pRequest->cScatterGatherEntries)
|
---|
113 | {
|
---|
114 | size_t cbToCopy = (cbBuf < pSGEntry->cbSeg) ? cbBuf : pSGEntry->cbSeg;
|
---|
115 |
|
---|
116 | memcpy(pSGEntry->pvSeg, pu8Buf, cbToCopy);
|
---|
117 |
|
---|
118 | cbBuf -= cbToCopy;
|
---|
119 | /* We finished. */
|
---|
120 | if (!cbBuf)
|
---|
121 | break;
|
---|
122 |
|
---|
123 | /* Advance the buffer. */
|
---|
124 | pu8Buf += cbToCopy;
|
---|
125 |
|
---|
126 | /* Go to the next entry in the list. */
|
---|
127 | pSGEntry++;
|
---|
128 | cSGEntry++;
|
---|
129 | }
|
---|
130 |
|
---|
131 | return VINF_SUCCESS;
|
---|
132 | }
|
---|
133 |
|
---|
134 | /**
|
---|
135 | * Set the sense and advanced sense key in the buffer for error conditions.
|
---|
136 | *
|
---|
137 | * @returns nothing.
|
---|
138 | * @param pRequest Pointer to the request which contains the sense buffer.
|
---|
139 | * @param uSCSISenseKey The sense key to set.
|
---|
140 | * @param uSCSIASC The advanced sense key to set.
|
---|
141 | */
|
---|
142 | DECLINLINE(void) drvscsiCmdError(PPDMSCSIREQUEST pRequest, uint8_t uSCSISenseKey, uint8_t uSCSIASC)
|
---|
143 | {
|
---|
144 | AssertMsg(pRequest->cbSenseBuffer >= 2, ("Sense buffer is not big enough\n"));
|
---|
145 | AssertMsg(pRequest->pbSenseBuffer, ("Sense buffer pointer is NULL\n"));
|
---|
146 | pRequest->pbSenseBuffer[0] = uSCSISenseKey;
|
---|
147 | pRequest->pbSenseBuffer[1] = uSCSIASC;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Sets the sense key for a status good condition.
|
---|
152 | *
|
---|
153 | * @returns nothing.
|
---|
154 | * @param pRequest Pointer to the request which contains the sense buffer.
|
---|
155 | */
|
---|
156 | DECLINLINE(void) drvscsihostCmdOk(PPDMSCSIREQUEST pRequest)
|
---|
157 | {
|
---|
158 | AssertMsg(pRequest->cbSenseBuffer >= 2, ("Sense buffer is not big enough\n"));
|
---|
159 | AssertMsg(pRequest->pbSenseBuffer, ("Sense buffer pointer is NULL\n"));
|
---|
160 | pRequest->pbSenseBuffer[0] = SCSI_SENSE_NONE;
|
---|
161 | pRequest->pbSenseBuffer[1] = SCSI_ASC_NONE;
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Returns the transfer direction of the given command
|
---|
166 | * in case the device does not provide this info.
|
---|
167 | *
|
---|
168 | * @returns transfer direction of the command.
|
---|
169 | * SCSIHOSTTXDIR_NONE if no data is transferred.
|
---|
170 | * SCSIHOSTTXDIR_FROM_DEVICE if the data is read from the device.
|
---|
171 | * SCSIHOSTTXDIR_TO_DEVICE if the data is written to the device.
|
---|
172 | * @param uCommand The command byte.
|
---|
173 | */
|
---|
174 | static unsigned drvscsihostGetTransferDirectionFromCommand(uint8_t uCommand)
|
---|
175 | {
|
---|
176 | switch (uCommand)
|
---|
177 | {
|
---|
178 | case SCSI_INQUIRY:
|
---|
179 | case SCSI_REPORT_LUNS:
|
---|
180 | case SCSI_MODE_SENSE_6:
|
---|
181 | case SCSI_READ_TOC_PMA_ATIP:
|
---|
182 | case SCSI_READ_CAPACITY:
|
---|
183 | case SCSI_MODE_SENSE_10:
|
---|
184 | case SCSI_GET_EVENT_STATUS_NOTIFICATION:
|
---|
185 | case SCSI_GET_CONFIGURATION:
|
---|
186 | case SCSI_READ_10:
|
---|
187 | case SCSI_READ_12:
|
---|
188 | case SCSI_READ_BUFFER:
|
---|
189 | case SCSI_READ_BUFFER_CAPACITY:
|
---|
190 | case SCSI_READ_DISC_INFORMATION:
|
---|
191 | case SCSI_READ_DVD_STRUCTURE:
|
---|
192 | case SCSI_READ_FORMAT_CAPACITIES:
|
---|
193 | case SCSI_READ_SUBCHANNEL:
|
---|
194 | case SCSI_READ_TRACK_INFORMATION:
|
---|
195 | case SCSI_READ_CD:
|
---|
196 | case SCSI_READ_CD_MSF:
|
---|
197 | return PDMSCSIREQUESTTXDIR_FROM_DEVICE;
|
---|
198 | case SCSI_TEST_UNIT_READY:
|
---|
199 | case SCSI_PREVENT_ALLOW_MEDIUM_REMOVAL:
|
---|
200 | case SCSI_START_STOP_UNIT:
|
---|
201 | return PDMSCSIREQUESTTXDIR_NONE;
|
---|
202 | case SCSI_WRITE_10:
|
---|
203 | case SCSI_WRITE_12:
|
---|
204 | case SCSI_WRITE_BUFFER:
|
---|
205 | return PDMSCSIREQUESTTXDIR_TO_DEVICE;
|
---|
206 | default:
|
---|
207 | AssertMsgFailed(("Command not known %#x\n", uCommand));
|
---|
208 | }
|
---|
209 |
|
---|
210 | /* We should never get here in debug mode. */
|
---|
211 | AssertMsgFailed(("Impossible to get here!!!\n"));
|
---|
212 | return PDMSCSIREQUESTTXDIR_NONE; /* to make compilers happy. */
|
---|
213 | }
|
---|
214 |
|
---|
215 | static int drvscsihostProcessRequestOne(PDRVSCSIHOST pThis, PPDMSCSIREQUEST pRequest)
|
---|
216 | {
|
---|
217 | int rc = VINF_SUCCESS;
|
---|
218 | unsigned uTxDir;
|
---|
219 |
|
---|
220 | LogFlowFunc(("Entered\n"));
|
---|
221 |
|
---|
222 | #ifdef DEBUG
|
---|
223 | drvscsihostDumpScsiRequest(pRequest);
|
---|
224 | #endif
|
---|
225 |
|
---|
226 | /* We implement only one device. */
|
---|
227 | if (pRequest->uLogicalUnit != 0)
|
---|
228 | {
|
---|
229 | switch (pRequest->pbCDB[0])
|
---|
230 | {
|
---|
231 | case SCSI_INQUIRY:
|
---|
232 | {
|
---|
233 | SCSIINQUIRYDATA ScsiInquiryReply;
|
---|
234 |
|
---|
235 | memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
|
---|
236 |
|
---|
237 | ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
|
---|
238 | ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
|
---|
239 | drvscsihostScatterGatherListCopyFromBuffer(pRequest, &ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
|
---|
240 | drvscsihostCmdOk(pRequest);
|
---|
241 | break;
|
---|
242 | }
|
---|
243 | default:
|
---|
244 | AssertMsgFailed(("Command not implemented for attached device\n"));
|
---|
245 | drvscsiCmdError(pRequest, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_NONE);
|
---|
246 | }
|
---|
247 | }
|
---|
248 | else
|
---|
249 | {
|
---|
250 | #if defined(RT_OS_LINUX)
|
---|
251 | sg_io_hdr_t ScsiIoReq;
|
---|
252 | sg_iovec_t *paSG = NULL;
|
---|
253 |
|
---|
254 | /* Setup SCSI request. */
|
---|
255 | memset(&ScsiIoReq, 0, sizeof(sg_io_hdr_t));
|
---|
256 | ScsiIoReq.interface_id = 'S';
|
---|
257 |
|
---|
258 | if (pRequest->uDataDirection == PDMSCSIREQUESTTXDIR_UNKNOWN)
|
---|
259 | uTxDir = drvscsihostGetTransferDirectionFromCommand(pRequest->pbCDB[0]);
|
---|
260 | else
|
---|
261 | uTxDir = pRequest->uDataDirection;
|
---|
262 |
|
---|
263 | if (uTxDir == PDMSCSIREQUESTTXDIR_NONE)
|
---|
264 | ScsiIoReq.dxfer_direction = SG_DXFER_NONE;
|
---|
265 | else if (uTxDir == PDMSCSIREQUESTTXDIR_TO_DEVICE)
|
---|
266 | ScsiIoReq.dxfer_direction = SG_DXFER_TO_DEV;
|
---|
267 | else if (uTxDir == PDMSCSIREQUESTTXDIR_FROM_DEVICE)
|
---|
268 | ScsiIoReq.dxfer_direction = SG_DXFER_FROM_DEV;
|
---|
269 | else
|
---|
270 | AssertMsgFailed(("Invalid transfer direction %u\n", uTxDir));
|
---|
271 |
|
---|
272 | ScsiIoReq.cmd_len = pRequest->cbCDB;
|
---|
273 | ScsiIoReq.mx_sb_len = pRequest->cbSenseBuffer;
|
---|
274 | ScsiIoReq.dxfer_len = pRequest->cbScatterGather;
|
---|
275 |
|
---|
276 | if (pRequest->cScatterGatherEntries > 0)
|
---|
277 | {
|
---|
278 | if (pRequest->cScatterGatherEntries == 1)
|
---|
279 | {
|
---|
280 | ScsiIoReq.iovec_count = 0;
|
---|
281 | ScsiIoReq.dxferp = pRequest->paScatterGatherHead[0].pvSeg;
|
---|
282 | }
|
---|
283 | else
|
---|
284 | {
|
---|
285 | ScsiIoReq.iovec_count = pRequest->cScatterGatherEntries;
|
---|
286 |
|
---|
287 | paSG = (sg_iovec_t *)RTMemAllocZ(pRequest->cScatterGatherEntries * sizeof(sg_iovec_t));
|
---|
288 | AssertReturn(paSG, VERR_NO_MEMORY);
|
---|
289 |
|
---|
290 | for (unsigned i = 0; i < pRequest->cScatterGatherEntries; i++)
|
---|
291 | {
|
---|
292 | paSG[i].iov_base = pRequest->paScatterGatherHead[i].pvSeg;
|
---|
293 | paSG[i].iov_len = pRequest->paScatterGatherHead[i].cbSeg;
|
---|
294 | }
|
---|
295 | ScsiIoReq.dxferp = paSG;
|
---|
296 | }
|
---|
297 | }
|
---|
298 |
|
---|
299 | ScsiIoReq.cmdp = pRequest->pbCDB;
|
---|
300 | ScsiIoReq.sbp = pRequest->pbSenseBuffer;
|
---|
301 | ScsiIoReq.timeout = UINT_MAX;
|
---|
302 | ScsiIoReq.flags |= SG_FLAG_DIRECT_IO;
|
---|
303 |
|
---|
304 | /* Issue command. */
|
---|
305 | rc = ioctl(RTFileToNative(pThis->hDeviceFile), SG_IO, &ScsiIoReq);
|
---|
306 | if (rc < 0)
|
---|
307 | {
|
---|
308 | AssertMsgFailed(("Ioctl failed with rc=%d\n", rc));
|
---|
309 | }
|
---|
310 |
|
---|
311 | /* Request processed successfully. */
|
---|
312 | Log(("Command successfully processed\n"));
|
---|
313 | if (ScsiIoReq.iovec_count > 0)
|
---|
314 | RTMemFree(paSG);
|
---|
315 | #endif
|
---|
316 | }
|
---|
317 | /* Notify device that request finished. */
|
---|
318 | rc = pThis->pDevScsiPort->pfnSCSIRequestCompleted(pThis->pDevScsiPort, pRequest, SCSI_STATUS_OK, false, VINF_SUCCESS);
|
---|
319 | AssertMsgRC(rc, ("Notifying device above failed rc=%Rrc\n", rc));
|
---|
320 |
|
---|
321 | return rc;
|
---|
322 |
|
---|
323 | }
|
---|
324 |
|
---|
325 | /**
|
---|
326 | * Request function to wakeup the thread.
|
---|
327 | *
|
---|
328 | * @returns VWRN_STATE_CHANGED.
|
---|
329 | */
|
---|
330 | static int drvscsihostAsyncIOLoopWakeupFunc(void)
|
---|
331 | {
|
---|
332 | return VWRN_STATE_CHANGED;
|
---|
333 | }
|
---|
334 |
|
---|
335 | /**
|
---|
336 | * The thread function which processes the requests asynchronously.
|
---|
337 | *
|
---|
338 | * @returns VBox status code.
|
---|
339 | * @param pDrvIns Pointer to the device instance data.
|
---|
340 | * @param pThread Pointer to the thread instance data.
|
---|
341 | */
|
---|
342 | static int drvscsihostAsyncIOLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
343 | {
|
---|
344 | int rc = VINF_SUCCESS;
|
---|
345 | PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
|
---|
346 |
|
---|
347 | LogFlowFunc(("Entering async IO loop.\n"));
|
---|
348 |
|
---|
349 | if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
|
---|
350 | return VINF_SUCCESS;
|
---|
351 |
|
---|
352 | while (pThread->enmState == PDMTHREADSTATE_RUNNING)
|
---|
353 | {
|
---|
354 | rc = RTReqQueueProcess(pThis->hQueueRequests, RT_INDEFINITE_WAIT);
|
---|
355 | AssertMsg(rc == VWRN_STATE_CHANGED, ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n", rc));
|
---|
356 | }
|
---|
357 |
|
---|
358 | return VINF_SUCCESS;
|
---|
359 | }
|
---|
360 |
|
---|
361 | static int drvscsihostAsyncIOLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
|
---|
362 | {
|
---|
363 | int rc;
|
---|
364 | PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
|
---|
365 | PRTREQ pReq;
|
---|
366 |
|
---|
367 | AssertReturn(pThis->hQueueRequests != NIL_RTREQQUEUE, VERR_INVALID_STATE);
|
---|
368 |
|
---|
369 | rc = RTReqQueueCall(pThis->hQueueRequests, &pReq, 10000 /* 10 sec. */, (PFNRT)drvscsihostAsyncIOLoopWakeupFunc, 0);
|
---|
370 | AssertMsgRC(rc, ("Inserting request into queue failed rc=%Rrc\n"));
|
---|
371 |
|
---|
372 | return rc;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /* -=-=-=-=- ISCSIConnector -=-=-=-=- */
|
---|
376 |
|
---|
377 | /** @copydoc PDMISCSICONNECTOR::pfnSCSIRequestSend. */
|
---|
378 | static DECLCALLBACK(int) drvscsihostRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
|
---|
379 | {
|
---|
380 | int rc;
|
---|
381 | PDRVSCSIHOST pThis = PDMISCSICONNECTOR_2_DRVSCSIHOST(pInterface);
|
---|
382 | PRTREQ pReq;
|
---|
383 |
|
---|
384 | AssertReturn(pThis->hQueueRequests != NIL_RTREQQUEUE, VERR_INVALID_STATE);
|
---|
385 |
|
---|
386 | rc = RTReqQueueCallEx(pThis->hQueueRequests, &pReq, 0, RTREQFLAGS_NO_WAIT, (PFNRT)drvscsihostProcessRequestOne, 2, pThis, pSCSIRequest);
|
---|
387 | AssertMsgReturn(RT_SUCCESS(rc), ("Inserting request into queue failed rc=%Rrc\n", rc), rc);
|
---|
388 |
|
---|
389 | return VINF_SUCCESS;
|
---|
390 | }
|
---|
391 |
|
---|
392 | /* -=-=-=-=- PDMIBASE -=-=-=-=- */
|
---|
393 |
|
---|
394 | /**
|
---|
395 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
396 | */
|
---|
397 | static DECLCALLBACK(void *) drvscsihostQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
398 | {
|
---|
399 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
400 | PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
|
---|
401 |
|
---|
402 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
403 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMISCSICONNECTOR, &pThis->ISCSIConnector);
|
---|
404 | return NULL;
|
---|
405 | }
|
---|
406 |
|
---|
407 | /* -=-=-=-=- PDMDRVREG -=-=-=-=- */
|
---|
408 |
|
---|
409 | /**
|
---|
410 | * Destruct a driver instance.
|
---|
411 | *
|
---|
412 | * Most VM resources are freed by the VM. This callback is provided so that any non-VM
|
---|
413 | * resources can be freed correctly.
|
---|
414 | *
|
---|
415 | * @param pDrvIns The driver instance data.
|
---|
416 | */
|
---|
417 | static DECLCALLBACK(void) drvscsihostDestruct(PPDMDRVINS pDrvIns)
|
---|
418 | {
|
---|
419 | PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
|
---|
420 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
421 |
|
---|
422 | RTFileClose(pThis->hDeviceFile);
|
---|
423 | pThis->hDeviceFile = NIL_RTFILE;
|
---|
424 |
|
---|
425 | if (pThis->pszDevicePath)
|
---|
426 | {
|
---|
427 | MMR3HeapFree(pThis->pszDevicePath);
|
---|
428 | pThis->pszDevicePath = NULL;
|
---|
429 | }
|
---|
430 |
|
---|
431 | if (pThis->hQueueRequests != NIL_RTREQQUEUE)
|
---|
432 | {
|
---|
433 | int rc = RTReqQueueDestroy(pThis->hQueueRequests);
|
---|
434 | AssertMsgRC(rc, ("Failed to destroy queue rc=%Rrc\n", rc));
|
---|
435 | pThis->hQueueRequests = NIL_RTREQQUEUE;
|
---|
436 | }
|
---|
437 |
|
---|
438 | }
|
---|
439 |
|
---|
440 | /**
|
---|
441 | * Construct a block driver instance.
|
---|
442 | *
|
---|
443 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
444 | */
|
---|
445 | static DECLCALLBACK(int) drvscsihostConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
446 | {
|
---|
447 | PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
|
---|
448 | LogFlowFunc(("pDrvIns=%#p pCfg=%#p\n", pDrvIns, pCfg));
|
---|
449 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
450 |
|
---|
451 | /*
|
---|
452 | * Initialize the instance data first because of the destructor.
|
---|
453 | */
|
---|
454 | pDrvIns->IBase.pfnQueryInterface = drvscsihostQueryInterface;
|
---|
455 | pThis->ISCSIConnector.pfnSCSIRequestSend = drvscsihostRequestSend;
|
---|
456 | pThis->pDrvIns = pDrvIns;
|
---|
457 | pThis->hDeviceFile = NIL_RTFILE;
|
---|
458 | pThis->hQueueRequests = NIL_RTREQQUEUE;
|
---|
459 |
|
---|
460 | /*
|
---|
461 | * Read the configuration.
|
---|
462 | */
|
---|
463 | if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
|
---|
464 | return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
|
---|
465 | N_("Invalid configuration for host scsi access driver"));
|
---|
466 |
|
---|
467 |
|
---|
468 | /* Query the SCSI port interface above. */
|
---|
469 | pThis->pDevScsiPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISCSIPORT);
|
---|
470 | AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
|
---|
471 |
|
---|
472 | /* Create request queue. */
|
---|
473 | int rc = RTReqQueueCreate(&pThis->hQueueRequests);
|
---|
474 | AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create request queue rc=%Rrc\n"), rc);
|
---|
475 |
|
---|
476 | /* Open the device. */
|
---|
477 | rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
|
---|
478 | if (RT_FAILURE(rc))
|
---|
479 | return PDMDRV_SET_ERROR(pDrvIns, rc,
|
---|
480 | N_("Configuration error: Failed to get the \"DevicePath\" value"));
|
---|
481 |
|
---|
482 | rc = RTFileOpen(&pThis->hDeviceFile, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
|
---|
483 | if (RT_FAILURE(rc))
|
---|
484 | return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
|
---|
485 | N_("DrvSCSIHost#%d: Failed to open device '%s'"), pDrvIns->iInstance, pThis->pszDevicePath);
|
---|
486 |
|
---|
487 | /* Create I/O thread. */
|
---|
488 | rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pAsyncIOThread, pThis, drvscsihostAsyncIOLoop,
|
---|
489 | drvscsihostAsyncIOLoopWakeup, 0, RTTHREADTYPE_IO, "SCSI async IO");
|
---|
490 | AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create async I/O thread rc=%Rrc\n"), rc);
|
---|
491 |
|
---|
492 | return VINF_SUCCESS;
|
---|
493 | }
|
---|
494 |
|
---|
495 | /**
|
---|
496 | * SCSI driver registration record.
|
---|
497 | */
|
---|
498 | const PDMDRVREG g_DrvSCSIHost =
|
---|
499 | {
|
---|
500 | /* u32Version */
|
---|
501 | PDM_DRVREG_VERSION,
|
---|
502 | /* szName */
|
---|
503 | "SCSIHost",
|
---|
504 | /* szRCMod */
|
---|
505 | "",
|
---|
506 | /* szR0Mod */
|
---|
507 | "",
|
---|
508 | /* pszDescription */
|
---|
509 | "Host SCSI driver.",
|
---|
510 | /* fFlags */
|
---|
511 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
512 | /* fClass. */
|
---|
513 | PDM_DRVREG_CLASS_SCSI,
|
---|
514 | /* cMaxInstances */
|
---|
515 | ~0U,
|
---|
516 | /* cbInstance */
|
---|
517 | sizeof(DRVSCSIHOST),
|
---|
518 | /* pfnConstruct */
|
---|
519 | drvscsihostConstruct,
|
---|
520 | /* pfnDestruct */
|
---|
521 | drvscsihostDestruct,
|
---|
522 | /* pfnRelocate */
|
---|
523 | NULL,
|
---|
524 | /* pfnIOCtl */
|
---|
525 | NULL,
|
---|
526 | /* pfnPowerOn */
|
---|
527 | NULL,
|
---|
528 | /* pfnReset */
|
---|
529 | NULL,
|
---|
530 | /* pfnSuspend */
|
---|
531 | NULL,
|
---|
532 | /* pfnResume */
|
---|
533 | NULL,
|
---|
534 | /* pfnAttach */
|
---|
535 | NULL,
|
---|
536 | /* pfnDetach */
|
---|
537 | NULL,
|
---|
538 | /* pfnPowerOff */
|
---|
539 | NULL,
|
---|
540 | /* pfnSoftReset */
|
---|
541 | NULL,
|
---|
542 | /* u32EndVersion */
|
---|
543 | PDM_DRVREG_VERSION
|
---|
544 | };
|
---|
545 |
|
---|