1 | /* $Id: VSCSIInternal.h 32983 2010-10-07 15:14:54Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: Internal defines
|
---|
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 | #ifndef ___VSCSIInternal_h
|
---|
18 | #define ___VSCSIInternal_h
|
---|
19 |
|
---|
20 | #include <VBox/vscsi.h>
|
---|
21 | #include <VBox/scsi.h>
|
---|
22 | #include <iprt/memcache.h>
|
---|
23 |
|
---|
24 | #include "VSCSIInline.h"
|
---|
25 |
|
---|
26 | /** Pointer to an internal virtual SCSI device. */
|
---|
27 | typedef VSCSIDEVICEINT *PVSCSIDEVICEINT;
|
---|
28 | /** Pointer to an internal virtual SCSI device LUN. */
|
---|
29 | typedef VSCSILUNINT *PVSCSILUNINT;
|
---|
30 | /** Pointer to an internal virtual SCSI device LUN pointer. */
|
---|
31 | typedef PVSCSILUNINT *PPVSCSILUNINT;
|
---|
32 | /** Pointer to a virtual SCSI LUN descriptor. */
|
---|
33 | typedef struct VSCSILUNDESC *PVSCSILUNDESC;
|
---|
34 | /** Pointer to a virtual SCSI request. */
|
---|
35 | typedef VSCSIREQINT *PVSCSIREQINT;
|
---|
36 | /** Pointer to a virtual SCSI I/O request. */
|
---|
37 | typedef VSCSIIOREQINT *PVSCSIIOREQINT;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Virtual SCSI device.
|
---|
41 | */
|
---|
42 | typedef struct VSCSIDEVICEINT
|
---|
43 | {
|
---|
44 | /** Request completion callback */
|
---|
45 | PFNVSCSIREQCOMPLETED pfnVScsiReqCompleted;
|
---|
46 | /** Opaque user data. */
|
---|
47 | void *pvVScsiDeviceUser;
|
---|
48 | /** Number of LUNs currently attached. */
|
---|
49 | uint32_t cLunsAttached;
|
---|
50 | /** How many LUNs are fitting in the array. */
|
---|
51 | uint32_t cLunsMax;
|
---|
52 | /** Request cache */
|
---|
53 | RTMEMCACHE hCacheReq;
|
---|
54 | /** Pointer to the array of LUN handles.
|
---|
55 | * The index is the LUN id. */
|
---|
56 | PPVSCSILUNINT papVScsiLun;
|
---|
57 | } VSCSIDEVICEINT;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Virtual SCSI device LUN.
|
---|
61 | */
|
---|
62 | typedef struct VSCSILUNINT
|
---|
63 | {
|
---|
64 | /** Pointer to the parent SCSI device. */
|
---|
65 | PVSCSIDEVICEINT pVScsiDevice;
|
---|
66 | /** Opaque user data */
|
---|
67 | void *pvVScsiLunUser;
|
---|
68 | /** I/O callback table */
|
---|
69 | PVSCSILUNIOCALLBACKS pVScsiLunIoCallbacks;
|
---|
70 | /** Pointer to the LUN type descriptor. */
|
---|
71 | PVSCSILUNDESC pVScsiLunDesc;
|
---|
72 | /** I/O request processing data */
|
---|
73 | struct
|
---|
74 | {
|
---|
75 | /** Number of outstanding tasks on this LUN. */
|
---|
76 | volatile uint32_t cReqOutstanding;
|
---|
77 | } IoReq;
|
---|
78 | } VSCSILUNINT;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * VSCSI Scatter/gather list handling
|
---|
82 | */
|
---|
83 | typedef struct VSCSIIOMEMCTX
|
---|
84 | {
|
---|
85 | /** Pointer to the scatter/gather list. */
|
---|
86 | PCRTSGSEG paDataSeg;
|
---|
87 | /** Number of segments. */
|
---|
88 | size_t cSegments;
|
---|
89 | /** Current segment we are in. */
|
---|
90 | unsigned iSegIdx;
|
---|
91 | /** Pointer to the current buffer. */
|
---|
92 | uint8_t *pbBuf;
|
---|
93 | /** Number of bytes left in the current buffer. */
|
---|
94 | size_t cbBufLeft;
|
---|
95 | } VSCSIIOMEMCTX;
|
---|
96 | /** Pointer to a I/O memory context. */
|
---|
97 | typedef VSCSIIOMEMCTX *PVSCSIIOMEMCTX;
|
---|
98 |
|
---|
99 | /**
|
---|
100 | * Virtual SCSI request.
|
---|
101 | */
|
---|
102 | typedef struct VSCSIREQINT
|
---|
103 | {
|
---|
104 | /** The LUN the request is for. */
|
---|
105 | uint32_t iLun;
|
---|
106 | /** The CDB */
|
---|
107 | uint8_t *pbCDB;
|
---|
108 | /** Size of the CDB */
|
---|
109 | size_t cbCDB;
|
---|
110 | /** I/O memory context */
|
---|
111 | VSCSIIOMEMCTX IoMemCtx;
|
---|
112 | /** Pointer to the sense buffer. */
|
---|
113 | uint8_t *pbSense;
|
---|
114 | /** Size of the sense buffer */
|
---|
115 | size_t cbSense;
|
---|
116 | /** Opaque user data associated with this request */
|
---|
117 | void *pvVScsiReqUser;
|
---|
118 | } VSCSIREQINT;
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Virtual SCSI I/O request.
|
---|
122 | */
|
---|
123 | typedef struct VSCSIIOREQINT
|
---|
124 | {
|
---|
125 | /** The associated request. */
|
---|
126 | PVSCSIREQINT pVScsiReq;
|
---|
127 | /** Lun for this I/O request. */
|
---|
128 | PVSCSILUNINT pVScsiLun;
|
---|
129 | /** Transfer direction */
|
---|
130 | VSCSIIOREQTXDIR enmTxDir;
|
---|
131 | /** Start offset */
|
---|
132 | uint64_t uOffset;
|
---|
133 | /** Number of bytes to transfer */
|
---|
134 | size_t cbTransfer;
|
---|
135 | /** Number of bytes the S/G list holds */
|
---|
136 | size_t cbSeg;
|
---|
137 | /** Number of segments. */
|
---|
138 | unsigned cSeg;
|
---|
139 | /** Segment array. */
|
---|
140 | PCRTSGSEG paSeg;
|
---|
141 | } VSCSIIOREQINT;
|
---|
142 |
|
---|
143 | /**
|
---|
144 | * Virtual SCSI LUN descriptor.
|
---|
145 | */
|
---|
146 | typedef struct VSCSILUNDESC
|
---|
147 | {
|
---|
148 | /** Device type this descriptor emulates. */
|
---|
149 | VSCSILUNTYPE enmLunType;
|
---|
150 | /** Descriptor name */
|
---|
151 | const char *pcszDescName;
|
---|
152 | /** LUN type size */
|
---|
153 | size_t cbLun;
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Initialise a Lun instance.
|
---|
157 | *
|
---|
158 | * @returns VBox status code.
|
---|
159 | * @param pVScsiLun The SCSI LUN instance.
|
---|
160 | */
|
---|
161 | DECLR3CALLBACKMEMBER(int, pfnVScsiLunInit, (PVSCSILUNINT pVScsiLun));
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Destroy a Lun instance.
|
---|
165 | *
|
---|
166 | * @returns VBox status code.
|
---|
167 | * @param pVScsiLun The SCSI LUN instance.
|
---|
168 | */
|
---|
169 | DECLR3CALLBACKMEMBER(int, pfnVScsiLunDestroy, (PVSCSILUNINT pVScsiLun));
|
---|
170 |
|
---|
171 | /**
|
---|
172 | * Processes a SCSI request.
|
---|
173 | *
|
---|
174 | * @returns VBox status code.
|
---|
175 | * @param pVScsiLun The SCSI LUN instance.
|
---|
176 | * @param pVScsiReq The SCSi request to process.
|
---|
177 | */
|
---|
178 | DECLR3CALLBACKMEMBER(int, pfnVScsiLunReqProcess, (PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq));
|
---|
179 |
|
---|
180 | } VSCSILUNDESC;
|
---|
181 |
|
---|
182 | /** Maximum number of LUNs a device can have. */
|
---|
183 | #define VSCSI_DEVICE_LUN_MAX 128
|
---|
184 |
|
---|
185 | /**
|
---|
186 | * Completesa SCSI request and calls the completion handler.
|
---|
187 | *
|
---|
188 | * @returns nothing.
|
---|
189 | * @param pVScsiDevice The virtual SCSI device.
|
---|
190 | * @param pVScsiReq The request which completed.
|
---|
191 | * @param rcScsiCode The status code
|
---|
192 | * One of the SCSI_STATUS_* #defines.
|
---|
193 | * @param fRedoPossible Flag whether redo is possible.
|
---|
194 | * @param rcReq Informational return code of the request.
|
---|
195 | */
|
---|
196 | void vscsiDeviceReqComplete(PVSCSIDEVICEINT pVScsiDevice, PVSCSIREQINT pVScsiReq,
|
---|
197 | int rcScsiCode, bool fRedoPossible, int rcReq);
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Initialize a I/O memory context.
|
---|
201 | *
|
---|
202 | * @returns nothing
|
---|
203 | * @param pIoMemCtx Pointer to a unitialized I/O memory context.
|
---|
204 | * @param paDataSeg Pointer to the S/G list.
|
---|
205 | * @param cSegments Number of segments in the S/G list.
|
---|
206 | */
|
---|
207 | void vscsiIoMemCtxInit(PVSCSIIOMEMCTX pIoMemCtx, PCRTSGSEG paDataSeg, size_t cSegments);
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Return a buffer from the I/O memory context.
|
---|
211 | *
|
---|
212 | * @returns Pointer to the buffer
|
---|
213 | * @param pIoMemCtx Pointer to the I/O memory context.
|
---|
214 | * @param pcbData Pointer to the amount of byte requested.
|
---|
215 | * If the current buffer doesn't have enough bytes left
|
---|
216 | * the amount is returned in the variable.
|
---|
217 | */
|
---|
218 | uint8_t *vscsiIoMemCtxGetBuffer(PVSCSIIOMEMCTX pIoMemCtx, size_t *pcbData);
|
---|
219 |
|
---|
220 | /**
|
---|
221 | * Copies data to a buffer described by a I/O memory context.
|
---|
222 | *
|
---|
223 | * @returns The amount of data copied before we run out of either
|
---|
224 | * I/O memory or src data.
|
---|
225 | * @param pIoMemCtx The I/O memory context to copy the data into.
|
---|
226 | * @param pbData Pointer to the data data to copy.
|
---|
227 | * @param cbData Amount of data to copy.
|
---|
228 | */
|
---|
229 | size_t vscsiCopyToIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData);
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * Copies data from a buffer described by a I/O memory context.
|
---|
233 | *
|
---|
234 | * @returns The amount of data copied before we run out of either
|
---|
235 | * I/O memory or dst data.
|
---|
236 | * @param pIoMemCtx The I/O memory context to copy the data from.
|
---|
237 | * @param pbData Pointer to the destination buffer.
|
---|
238 | * @param cbData Amount of data to copy.
|
---|
239 | */
|
---|
240 | size_t vscsiCopyFromIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData);
|
---|
241 |
|
---|
242 | /**
|
---|
243 | * Sets a ok sense code.
|
---|
244 | *
|
---|
245 | * @returns SCSI status code.
|
---|
246 | * @param pVScsiReq The SCSI request.
|
---|
247 | */
|
---|
248 | int vscsiReqSenseOkSet(PVSCSIREQINT pVScsiReq);
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Sets a error sense code.
|
---|
252 | *
|
---|
253 | * @returns SCSI status code.
|
---|
254 | * @param pVScsiReq The SCSI request.
|
---|
255 | * @param uSCSISenseKey The SCSi sense key to set.
|
---|
256 | * @param uSCSIASC The ASC value.
|
---|
257 | */
|
---|
258 | int vscsiReqSenseErrorSet(PVSCSIREQINT pVScsiReq, uint8_t uSCSISenseKey, uint8_t uSCSIASC);
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Enqueues a new flush request
|
---|
262 | *
|
---|
263 | * @retruns VBox status code.
|
---|
264 | * @param pVScsiLun The LUN instance which issued the request.
|
---|
265 | * @param pVScsiReq The virtual SCSI request associated with the flush.
|
---|
266 | */
|
---|
267 | int vscsiIoReqFlushEnqueue(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq);
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * Enqueue a new data transfer request.
|
---|
271 | *
|
---|
272 | * @returns VBox status code.
|
---|
273 | * @param pVScsiLun The LUN instance which issued the request.
|
---|
274 | * @param pVScsiReq The virtual SCSI request associated with the transfer.
|
---|
275 | * @param enmTxDir Transfer direction.
|
---|
276 | * @param uOffset Start offset of the transfer.
|
---|
277 | * @param cbTransfer Number of bytes to transfer.
|
---|
278 | */
|
---|
279 | int vscsiIoReqTransferEnqueue(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq,
|
---|
280 | VSCSIIOREQTXDIR enmTxDir, uint64_t uOffset,
|
---|
281 | size_t cbTransfer);
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Returns the current number of outstanding tasks on the given LUN.
|
---|
285 | *
|
---|
286 | * @returns Number of outstanding tasks.
|
---|
287 | * @param pVScsiLun The LUN to check.
|
---|
288 | */
|
---|
289 | uint32_t vscsiIoReqOutstandingCountGet(PVSCSILUNINT pVScsiLun);
|
---|
290 |
|
---|
291 | DECLINLINE(int) vscsiLunMediumGetSize(PVSCSILUNINT pVScsiLun, uint64_t *pcbSize)
|
---|
292 | {
|
---|
293 | return pVScsiLun->pVScsiLunIoCallbacks->pfnVScsiLunMediumGetSize(pVScsiLun,
|
---|
294 | pVScsiLun->pvVScsiLunUser,
|
---|
295 | pcbSize);
|
---|
296 | }
|
---|
297 |
|
---|
298 | DECLINLINE(int) vscsiLunReqTransferEnqueue(PVSCSILUNINT pVScsiLun, PVSCSIIOREQINT pVScsiIoReq)
|
---|
299 | {
|
---|
300 | return pVScsiLun->pVScsiLunIoCallbacks->pfnVScsiLunReqTransferEnqueue(pVScsiLun,
|
---|
301 | pVScsiLun->pvVScsiLunUser,
|
---|
302 | pVScsiIoReq);
|
---|
303 | }
|
---|
304 |
|
---|
305 | #endif /* ___VSCSIInternal_h */
|
---|
306 |
|
---|