1 | /* $Id: VSCSISgBuf.cpp 28800 2010-04-27 08:22:32Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: S/G list 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 <iprt/assert.h>
|
---|
20 | #include <iprt/string.h>
|
---|
21 |
|
---|
22 | #include "VSCSIInternal.h"
|
---|
23 |
|
---|
24 |
|
---|
25 | void vscsiIoMemCtxInit(PVSCSIIOMEMCTX pIoMemCtx, PCRTSGSEG paDataSeg, size_t cSegments)
|
---|
26 | {
|
---|
27 | if (RT_UNLIKELY(!cSegments))
|
---|
28 | {
|
---|
29 | pIoMemCtx->paDataSeg = NULL;
|
---|
30 | pIoMemCtx->cSegments = 0;
|
---|
31 | pIoMemCtx->iSegIdx = 0;
|
---|
32 | pIoMemCtx->pbBuf = NULL;
|
---|
33 | pIoMemCtx->cbBufLeft = 0;
|
---|
34 | }
|
---|
35 | else
|
---|
36 | {
|
---|
37 | pIoMemCtx->paDataSeg = paDataSeg;
|
---|
38 | pIoMemCtx->cSegments = cSegments;
|
---|
39 | pIoMemCtx->iSegIdx = 0;
|
---|
40 | pIoMemCtx->pbBuf = (uint8_t *)paDataSeg[0].pvSeg;
|
---|
41 | pIoMemCtx->cbBufLeft = paDataSeg[0].cbSeg;
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 |
|
---|
46 | uint8_t *vscsiIoMemCtxGetBuffer(PVSCSIIOMEMCTX pIoMemCtx, size_t *pcbData)
|
---|
47 | {
|
---|
48 | size_t cbData = RT_MIN(*pcbData, pIoMemCtx->cbBufLeft);
|
---|
49 | uint8_t *pbBuf = pIoMemCtx->pbBuf;
|
---|
50 |
|
---|
51 | pIoMemCtx->cbBufLeft -= cbData;
|
---|
52 |
|
---|
53 | /* Advance to the next segment if required. */
|
---|
54 | if (!pIoMemCtx->cbBufLeft)
|
---|
55 | {
|
---|
56 | pIoMemCtx->iSegIdx++;
|
---|
57 |
|
---|
58 | if (RT_UNLIKELY(pIoMemCtx->iSegIdx == pIoMemCtx->cSegments))
|
---|
59 | {
|
---|
60 | pIoMemCtx->cbBufLeft = 0;
|
---|
61 | pIoMemCtx->pbBuf = NULL;
|
---|
62 | }
|
---|
63 | else
|
---|
64 | {
|
---|
65 | pIoMemCtx->pbBuf = (uint8_t *)pIoMemCtx->paDataSeg[pIoMemCtx->iSegIdx].pvSeg;
|
---|
66 | pIoMemCtx->cbBufLeft = pIoMemCtx->paDataSeg[pIoMemCtx->iSegIdx].cbSeg;
|
---|
67 | }
|
---|
68 |
|
---|
69 | *pcbData = cbData;
|
---|
70 | }
|
---|
71 | else
|
---|
72 | pIoMemCtx->pbBuf += cbData;
|
---|
73 |
|
---|
74 | return pbBuf;
|
---|
75 | }
|
---|
76 |
|
---|
77 |
|
---|
78 | size_t vscsiCopyToIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData)
|
---|
79 | {
|
---|
80 | size_t cbLeft = cbData;
|
---|
81 |
|
---|
82 | while (cbLeft)
|
---|
83 | {
|
---|
84 | size_t cbCopy = cbLeft;
|
---|
85 | uint8_t *pbBuf = vscsiIoMemCtxGetBuffer(pIoMemCtx, &cbCopy);
|
---|
86 |
|
---|
87 | if (!cbCopy)
|
---|
88 | break;
|
---|
89 |
|
---|
90 | memcpy(pbBuf, pbData, cbCopy);
|
---|
91 |
|
---|
92 | cbLeft -= cbCopy;
|
---|
93 | pbData += cbCopy;
|
---|
94 | }
|
---|
95 |
|
---|
96 | return cbData - cbLeft;
|
---|
97 | }
|
---|
98 |
|
---|
99 |
|
---|
100 | size_t vscsiCopyFromIoMemCtx(PVSCSIIOMEMCTX pIoMemCtx, uint8_t *pbData, size_t cbData)
|
---|
101 | {
|
---|
102 | size_t cbLeft = cbData;
|
---|
103 |
|
---|
104 | while (cbLeft)
|
---|
105 | {
|
---|
106 | size_t cbCopy = cbData;
|
---|
107 | uint8_t *pbBuf = vscsiIoMemCtxGetBuffer(pIoMemCtx, &cbCopy);
|
---|
108 |
|
---|
109 | if (!cbCopy)
|
---|
110 | break;
|
---|
111 |
|
---|
112 | memcpy(pbData, pbBuf, cbCopy);
|
---|
113 |
|
---|
114 | cbData -= cbCopy;
|
---|
115 | pbData += cbCopy;
|
---|
116 | }
|
---|
117 |
|
---|
118 | return cbData - cbLeft;
|
---|
119 | }
|
---|
120 |
|
---|