1 | /* $Id: VSCSILun.cpp 27653 2010-03-23 23:28:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: LUN handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2010 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 | #define LOG_GROUP LOG_GROUP_VSCSI
|
---|
22 | #include <VBox/log.h>
|
---|
23 | #include <VBox/err.h>
|
---|
24 | #include <VBox/types.h>
|
---|
25 | #include <VBox/vscsi.h>
|
---|
26 | #include <iprt/assert.h>
|
---|
27 | #include <iprt/mem.h>
|
---|
28 |
|
---|
29 | #include "VSCSIInternal.h"
|
---|
30 |
|
---|
31 | /** SBC descriptor */
|
---|
32 | extern VSCSILUNDESC g_VScsiLunTypeSbc;
|
---|
33 | /** MMC descriptor */
|
---|
34 | //extern PVSCSILUNDESC g_pVScsiLunTypeMmc;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Array of supported SCSI LUN types.
|
---|
38 | */
|
---|
39 | static PVSCSILUNDESC g_aVScsiLunTypesSupported[] =
|
---|
40 | {
|
---|
41 | &g_VScsiLunTypeSbc
|
---|
42 | };
|
---|
43 |
|
---|
44 | VBOXDDU_DECL(int) VSCSILunCreate(PVSCSILUN phVScsiLun, VSCSILUNTYPE enmLunType,
|
---|
45 | PVSCSILUNIOCALLBACKS pVScsiLunIoCallbacks,
|
---|
46 | void *pvVScsiLunUser)
|
---|
47 | {
|
---|
48 | PVSCSILUNINT pVScsiLun = NULL;
|
---|
49 | PVSCSILUNDESC pVScsiLunDesc = NULL;
|
---|
50 |
|
---|
51 | AssertPtrReturn(phVScsiLun, VERR_INVALID_POINTER);
|
---|
52 | AssertReturn( enmLunType > VSCSILUNTYPE_INVALID
|
---|
53 | && enmLunType < VSCSILUNTYPE_LAST, VERR_INVALID_PARAMETER);
|
---|
54 | AssertPtrReturn(pVScsiLunIoCallbacks, VERR_INVALID_PARAMETER);
|
---|
55 |
|
---|
56 | for (unsigned idxLunType = 0; idxLunType < RT_ELEMENTS(g_aVScsiLunTypesSupported); idxLunType++)
|
---|
57 | {
|
---|
58 | if (g_aVScsiLunTypesSupported[idxLunType]->enmLunType == enmLunType)
|
---|
59 | {
|
---|
60 | pVScsiLunDesc = g_aVScsiLunTypesSupported[idxLunType];
|
---|
61 | break;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | if (!pVScsiLunDesc)
|
---|
66 | return VERR_VSCSI_LUN_TYPE_NOT_SUPPORTED;
|
---|
67 |
|
---|
68 | pVScsiLun = (PVSCSILUNINT)RTMemAllocZ(pVScsiLunDesc->cbLun);
|
---|
69 | if (!pVScsiLun)
|
---|
70 | return VERR_NO_MEMORY;
|
---|
71 |
|
---|
72 | pVScsiLun->pVScsiDevice = NULL;
|
---|
73 | pVScsiLun->pvVScsiLunUser = pvVScsiLunUser;
|
---|
74 | pVScsiLun->pVScsiLunIoCallbacks = pVScsiLunIoCallbacks;
|
---|
75 | pVScsiLun->pVScsiLunDesc = pVScsiLunDesc;
|
---|
76 |
|
---|
77 | int rc = pVScsiLunDesc->pfnVScsiLunInit(pVScsiLun);
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | {
|
---|
80 | /** @todo Init other stuff */
|
---|
81 | *phVScsiLun = pVScsiLun;
|
---|
82 | return VINF_SUCCESS;
|
---|
83 | }
|
---|
84 |
|
---|
85 | RTMemFree(pVScsiLun);
|
---|
86 |
|
---|
87 | return rc;
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Destroy virtual SCSI LUN.
|
---|
92 | *
|
---|
93 | * @returns VBox status code.
|
---|
94 | * @param hVScsiLun The virtal SCSI LUN handle to destroy.
|
---|
95 | */
|
---|
96 | VBOXDDU_DECL(int) VSCSILunDestroy(VSCSILUN hVScsiLun)
|
---|
97 | {
|
---|
98 | PVSCSILUNINT pVScsiLun = (PVSCSILUNINT)hVScsiLun;
|
---|
99 |
|
---|
100 | AssertPtrReturn(pVScsiLun, VERR_INVALID_HANDLE);
|
---|
101 | AssertReturn(!pVScsiLun->pVScsiDevice, VERR_VSCSI_LUN_ATTACHED_TO_DEVICE);
|
---|
102 | AssertReturn(vscsiIoReqOutstandingCountGet(pVScsiLun) != 0, VERR_VSCSI_LUN_BUSY);
|
---|
103 |
|
---|
104 | int rc = pVScsiLun->pVScsiLunDesc->pfnVScsiLunDestroy(pVScsiLun);
|
---|
105 | if (RT_FAILURE(rc))
|
---|
106 | return rc;
|
---|
107 |
|
---|
108 | /* Make LUN invalid */
|
---|
109 | pVScsiLun->pvVScsiLunUser = NULL;
|
---|
110 | pVScsiLun->pVScsiLunIoCallbacks = NULL;
|
---|
111 | pVScsiLun->pVScsiLunDesc = NULL;
|
---|
112 |
|
---|
113 | RTMemFree(pVScsiLun);
|
---|
114 |
|
---|
115 | return VINF_SUCCESS;
|
---|
116 | }
|
---|
117 |
|
---|