1 | /* $Id: VSCSILun.cpp 33540 2010-10-28 09:27:05Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: LUN 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 |
|
---|
25 | #include "VSCSIInternal.h"
|
---|
26 |
|
---|
27 | /** SBC descriptor */
|
---|
28 | extern VSCSILUNDESC g_VScsiLunTypeSbc;
|
---|
29 | /** MMC descriptor */
|
---|
30 | //extern PVSCSILUNDESC g_pVScsiLunTypeMmc;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Array of supported SCSI LUN types.
|
---|
34 | */
|
---|
35 | static PVSCSILUNDESC g_aVScsiLunTypesSupported[] =
|
---|
36 | {
|
---|
37 | &g_VScsiLunTypeSbc
|
---|
38 | };
|
---|
39 |
|
---|
40 | VBOXDDU_DECL(int) VSCSILunCreate(PVSCSILUN phVScsiLun, VSCSILUNTYPE enmLunType,
|
---|
41 | PVSCSILUNIOCALLBACKS pVScsiLunIoCallbacks,
|
---|
42 | void *pvVScsiLunUser)
|
---|
43 | {
|
---|
44 | PVSCSILUNINT pVScsiLun = NULL;
|
---|
45 | PVSCSILUNDESC pVScsiLunDesc = NULL;
|
---|
46 |
|
---|
47 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
48 | AssertReturn( enmLunType > VSCSILUNTYPE_INVALID
|
---|
49 | && enmLunType < VSCSILUNTYPE_LAST, VERR_INVALID_PARAMETER);
|
---|
50 | AssertPtrReturn(pVScsiLunIoCallbacks, VERR_INVALID_PARAMETER);
|
---|
51 |
|
---|
52 | for (unsigned idxLunType = 0; idxLunType < RT_ELEMENTS(g_aVScsiLunTypesSupported); idxLunType++)
|
---|
53 | {
|
---|
54 | if (g_aVScsiLunTypesSupported[idxLunType]->enmLunType == enmLunType)
|
---|
55 | {
|
---|
56 | pVScsiLunDesc = g_aVScsiLunTypesSupported[idxLunType];
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (!pVScsiLunDesc)
|
---|
62 | return VERR_VSCSI_LUN_TYPE_NOT_SUPPORTED;
|
---|
63 |
|
---|
64 | pVScsiLun = (PVSCSILUNINT)RTMemAllocZ(pVScsiLunDesc->cbLun);
|
---|
65 | if (!pVScsiLun)
|
---|
66 | return VERR_NO_MEMORY;
|
---|
67 |
|
---|
68 | pVScsiLun->pVScsiDevice = NULL;
|
---|
69 | pVScsiLun->pvVScsiLunUser = pvVScsiLunUser;
|
---|
70 | pVScsiLun->pVScsiLunIoCallbacks = pVScsiLunIoCallbacks;
|
---|
71 | pVScsiLun->pVScsiLunDesc = pVScsiLunDesc;
|
---|
72 |
|
---|
73 | int rc = pVScsiLunDesc->pfnVScsiLunInit(pVScsiLun);
|
---|
74 | if (RT_SUCCESS(rc))
|
---|
75 | {
|
---|
76 | /** @todo Init other stuff */
|
---|
77 | *phVScsiLun = pVScsiLun;
|
---|
78 | return VINF_SUCCESS;
|
---|
79 | }
|
---|
80 |
|
---|
81 | RTMemFree(pVScsiLun);
|
---|
82 |
|
---|
83 | return rc;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Destroy virtual SCSI LUN.
|
---|
88 | *
|
---|
89 | * @returns VBox status code.
|
---|
90 | * @param hVScsiLun The virtual SCSI LUN handle to destroy.
|
---|
91 | */
|
---|
92 | VBOXDDU_DECL(int) VSCSILunDestroy(VSCSILUN hVScsiLun)
|
---|
93 | {
|
---|
94 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
95 |
|
---|
96 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
97 | AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
|
---|
98 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) == 0, VERR_VSCSI_LUN_BUSY);
|
---|
99 |
|
---|
100 | int rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunDestroy(pVScsiLun);
|
---|
101 | if (RT_FAILURE(rc))
|
---|
102 | return rc;
|
---|
103 |
|
---|
104 | /* Make LUN invalid */
|
---|
105 | pVScsiLun->pvVScsiLunUser = NULL;
|
---|
106 | pVScsiLun->pVScsiLunIoCallbacks = NULL;
|
---|
107 | pVScsiLun->pVScsiLunDesc = NULL;
|
---|
108 |
|
---|
109 | RTMemFree(pVScsiLun);
|
---|
110 |
|
---|
111 | return VINF_SUCCESS;
|
---|
112 | }
|
---|
113 |
|
---|