VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvSCSIHost.cpp@ 31193

Last change on this file since 31193 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: 17.2 KB
Line 
1/* $Id: DrvSCSIHost.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
2/** @file
3 * VBox storage drivers: Host SCSI access driver.
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
18/*******************************************************************************
19* Header Files *
20*******************************************************************************/
21//#define DEBUG
22#define LOG_GROUP LOG_GROUP_DRV_SCSIHOST
23#include <VBox/pdmdrv.h>
24#include <VBox/pdmifs.h>
25#include <VBox/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 "../Builtins.h"
41
42/**
43 * SCSI driver instance data.
44 *
45 * @implements PDMISCSICONNECTOR
46 */
47typedef 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 DeviceFile;
61
62 /** The dedicated I/O thread. */
63 PPDMTHREAD pAsyncIOThread;
64 /** Queue for passing the requests to the thread. */
65 PRTREQQUEUE pQueueRequests;
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 */
78static 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 */
106static 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 */
142DECLINLINE(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 */
156DECLINLINE(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 transfered.
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 */
174static 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
215static 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 AssertPtrReturn(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(pThis->DeviceFile, 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);
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 */
330static 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 */
342static 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 = RTReqProcess(pThis->pQueueRequests, 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
361static int drvscsihostAsyncIOLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
362{
363 int rc;
364 PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
365 PRTREQ pReq;
366
367 AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
368
369 rc = RTReqCall(pThis->pQueueRequests, &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. */
378static DECLCALLBACK(int) drvscsihostRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
379{
380 int rc;
381 PDRVSCSIHOST pThis = PDMISCSICONNECTOR_2_DRVSCSIHOST(pInterface);
382 PRTREQ pReq;
383
384 AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
385
386 rc = RTReqCallEx(pThis->pQueueRequests, &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 */
397static 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 */
417static DECLCALLBACK(void) drvscsihostDestruct(PPDMDRVINS pDrvIns)
418{
419 PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
420 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
421
422 if (pThis->DeviceFile != NIL_RTFILE)
423 RTFileClose(pThis->DeviceFile);
424
425 if (pThis->pszDevicePath)
426 MMR3HeapFree(pThis->pszDevicePath);
427
428 if (pThis->pQueueRequests)
429 {
430 int rc = RTReqDestroyQueue(pThis->pQueueRequests);
431 AssertMsgRC(rc, ("Failed to destroy queue rc=%Rrc\n", rc));
432 }
433
434}
435
436/**
437 * Construct a block driver instance.
438 *
439 * @copydoc FNPDMDRVCONSTRUCT
440 */
441static DECLCALLBACK(int) drvscsihostConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
442{
443 PDRVSCSIHOST pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSIHOST);
444 LogFlowFunc(("pDrvIns=%#p pCfg=%#p\n", pDrvIns, pCfg));
445 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
446
447 /*
448 * Read the configuration.
449 */
450 if (!CFGMR3AreValuesValid(pCfg, "DevicePath\0"))
451 return PDMDRV_SET_ERROR(pDrvIns, VERR_PDM_DRVINS_UNKNOWN_CFG_VALUES,
452 N_("Invalid configuration for host scsi access driver"));
453
454 /*
455 * Initialize interfaces.
456 */
457 pDrvIns->IBase.pfnQueryInterface = drvscsihostQueryInterface;
458 pThis->ISCSIConnector.pfnSCSIRequestSend = drvscsihostRequestSend;
459 pThis->pDrvIns = pDrvIns;
460 pThis->DeviceFile = NIL_RTFILE;
461
462 /* Query the SCSI port interface above. */
463 pThis->pDevScsiPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMISCSIPORT);
464 AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
465
466 /* Create request queue. */
467 int rc = RTReqCreateQueue(&pThis->pQueueRequests);
468 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create request queue rc=%Rrc\n"), rc);
469
470 /* Open the device. */
471 rc = CFGMR3QueryStringAlloc(pCfg, "DevicePath", &pThis->pszDevicePath);
472 if (RT_FAILURE(rc))
473 return PDMDRV_SET_ERROR(pDrvIns, rc,
474 N_("Configuration error: Failed to get the \"DevicePath\" value"));
475
476 rc = RTFileOpen(&pThis->DeviceFile, pThis->pszDevicePath, RTFILE_O_READWRITE | RTFILE_O_OPEN | RTFILE_O_DENY_NONE);
477 if (RT_FAILURE(rc))
478 return PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS,
479 N_("DrvSCSIHost#%d: Failed to open device '%s'"), pDrvIns->iInstance, pThis->pszDevicePath);
480
481 /* Create I/O thread. */
482 rc = PDMDrvHlpThreadCreate(pDrvIns, &pThis->pAsyncIOThread, pThis, drvscsihostAsyncIOLoop,
483 drvscsihostAsyncIOLoopWakeup, 0, RTTHREADTYPE_IO, "SCSI async IO");
484 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create async I/O thread rc=%Rrc\n"), rc);
485
486 return VINF_SUCCESS;
487}
488
489/**
490 * SCSI driver registration record.
491 */
492const PDMDRVREG g_DrvSCSIHost =
493{
494 /* u32Version */
495 PDM_DRVREG_VERSION,
496 /* szName */
497 "SCSIHost",
498 /* szRCMod */
499 "",
500 /* szR0Mod */
501 "",
502 /* pszDescription */
503 "Host SCSI driver.",
504 /* fFlags */
505 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
506 /* fClass. */
507 PDM_DRVREG_CLASS_SCSI,
508 /* cMaxInstances */
509 ~0,
510 /* cbInstance */
511 sizeof(DRVSCSIHOST),
512 /* pfnConstruct */
513 drvscsihostConstruct,
514 /* pfnDestruct */
515 drvscsihostDestruct,
516 /* pfnRelocate */
517 NULL,
518 /* pfnIOCtl */
519 NULL,
520 /* pfnPowerOn */
521 NULL,
522 /* pfnReset */
523 NULL,
524 /* pfnSuspend */
525 NULL,
526 /* pfnResume */
527 NULL,
528 /* pfnAttach */
529 NULL,
530 /* pfnDetach */
531 NULL,
532 /* pfnPowerOff */
533 NULL,
534 /* pfnSoftReset */
535 NULL,
536 /* u32EndVersion */
537 PDM_DRVREG_VERSION
538};
539
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