VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VBoxSCSI.cpp@ 78272

Last change on this file since 78272 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Id: VBoxSCSI.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * VBox storage devices - Simple SCSI interface for BIOS access.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22//#define DEBUG
23#define LOG_GROUP LOG_GROUP_DEV_BUSLOGIC /** @todo Create extra group. */
24
25#if defined(IN_R0) || defined(IN_RC)
26# error This device has no R0 or RC components
27#endif
28
29#include <VBox/vmm/pdmdev.h>
30#include <VBox/vmm/pgm.h>
31#include <VBox/version.h>
32#include <iprt/asm.h>
33#include <iprt/mem.h>
34#include <iprt/thread.h>
35#include <iprt/string.h>
36
37#include "VBoxSCSI.h"
38
39
40/**
41 * Resets the state.
42 */
43static void vboxscsiReset(PVBOXSCSI pVBoxSCSI, bool fEverything)
44{
45 if (fEverything)
46 {
47 pVBoxSCSI->regIdentify = 0;
48 pVBoxSCSI->fBusy = false;
49 }
50 pVBoxSCSI->cbCDB = 0;
51 RT_ZERO(pVBoxSCSI->abCDB);
52 pVBoxSCSI->iCDB = 0;
53 pVBoxSCSI->rcCompletion = 0;
54 pVBoxSCSI->uTargetDevice = 0;
55 pVBoxSCSI->cbBuf = 0;
56 pVBoxSCSI->cbBufLeft = 0;
57 pVBoxSCSI->iBuf = 0;
58 if (pVBoxSCSI->pbBuf)
59 RTMemFree(pVBoxSCSI->pbBuf);
60 pVBoxSCSI->pbBuf = NULL;
61 pVBoxSCSI->enmState = VBOXSCSISTATE_NO_COMMAND;
62}
63
64/**
65 * Initializes the state for the SCSI interface.
66 *
67 * @returns VBox status code.
68 * @param pVBoxSCSI Pointer to the unitialized SCSI state.
69 */
70int vboxscsiInitialize(PVBOXSCSI pVBoxSCSI)
71{
72 pVBoxSCSI->pbBuf = NULL;
73 vboxscsiReset(pVBoxSCSI, true /*fEverything*/);
74
75 return VINF_SUCCESS;
76}
77
78/**
79 * Reads a register value.
80 *
81 * @returns VBox status code.
82 * @param pVBoxSCSI Pointer to the SCSI state.
83 * @param iRegister Index of the register to read.
84 * @param pu32Value Where to store the content of the register.
85 */
86int vboxscsiReadRegister(PVBOXSCSI pVBoxSCSI, uint8_t iRegister, uint32_t *pu32Value)
87{
88 uint8_t uVal = 0;
89
90 switch (iRegister)
91 {
92 case 0:
93 {
94 if (ASMAtomicReadBool(&pVBoxSCSI->fBusy) == true)
95 {
96 uVal |= VBOX_SCSI_BUSY;
97 /* There is an I/O operation in progress.
98 * Yield the execution thread to let the I/O thread make progress.
99 */
100 RTThreadYield();
101 }
102 if (pVBoxSCSI->rcCompletion)
103 uVal |= VBOX_SCSI_ERROR;
104 break;
105 }
106 case 1:
107 {
108 /* If we're not in the 'command ready' state, there may not even be a buffer yet. */
109 if ( pVBoxSCSI->enmState == VBOXSCSISTATE_COMMAND_READY
110 && pVBoxSCSI->cbBufLeft > 0)
111 {
112 AssertMsg(pVBoxSCSI->pbBuf, ("pBuf is NULL\n"));
113 Assert(!pVBoxSCSI->fBusy);
114 uVal = pVBoxSCSI->pbBuf[pVBoxSCSI->iBuf];
115 pVBoxSCSI->iBuf++;
116 pVBoxSCSI->cbBufLeft--;
117
118 /* When the guest reads the last byte from the data in buffer, clear
119 everything and reset command buffer. */
120 if (pVBoxSCSI->cbBufLeft == 0)
121 vboxscsiReset(pVBoxSCSI, false /*fEverything*/);
122 }
123 break;
124 }
125 case 2:
126 {
127 uVal = pVBoxSCSI->regIdentify;
128 break;
129 }
130 case 3:
131 {
132 uVal = pVBoxSCSI->rcCompletion;
133 break;
134 }
135 default:
136 AssertMsgFailed(("Invalid register to read from %u\n", iRegister));
137 }
138
139 *pu32Value = uVal;
140
141 return VINF_SUCCESS;
142}
143
144/**
145 * Writes to a register.
146 *
147 * @returns VBox status code.
148 * @retval VERR_MORE_DATA if a command is ready to be sent to the SCSI driver.
149 * @param pVBoxSCSI Pointer to the SCSI state.
150 * @param iRegister Index of the register to write to.
151 * @param uVal Value to write.
152 */
153int vboxscsiWriteRegister(PVBOXSCSI pVBoxSCSI, uint8_t iRegister, uint8_t uVal)
154{
155 int rc = VINF_SUCCESS;
156
157 switch (iRegister)
158 {
159 case 0:
160 {
161 if (pVBoxSCSI->enmState == VBOXSCSISTATE_NO_COMMAND)
162 {
163 pVBoxSCSI->enmState = VBOXSCSISTATE_READ_TXDIR;
164 pVBoxSCSI->uTargetDevice = uVal;
165 }
166 else if (pVBoxSCSI->enmState == VBOXSCSISTATE_READ_TXDIR)
167 {
168 if (uVal != VBOXSCSI_TXDIR_FROM_DEVICE && uVal != VBOXSCSI_TXDIR_TO_DEVICE)
169 vboxscsiReset(pVBoxSCSI, true /*fEverything*/);
170 else
171 {
172 pVBoxSCSI->enmState = VBOXSCSISTATE_READ_CDB_SIZE_BUFHI;
173 pVBoxSCSI->uTxDir = uVal;
174 }
175 }
176 else if (pVBoxSCSI->enmState == VBOXSCSISTATE_READ_CDB_SIZE_BUFHI)
177 {
178 uint8_t cbCDB = uVal & 0x0F;
179
180 if (cbCDB == 0)
181 cbCDB = 16;
182 if (cbCDB > VBOXSCSI_CDB_SIZE_MAX)
183 vboxscsiReset(pVBoxSCSI, true /*fEverything*/);
184 else
185 {
186 pVBoxSCSI->enmState = VBOXSCSISTATE_READ_BUFFER_SIZE_LSB;
187 pVBoxSCSI->cbCDB = cbCDB;
188 pVBoxSCSI->cbBuf = (uVal & 0xF0) << 12; /* Bits 16-19 of buffer size. */
189 }
190 }
191 else if (pVBoxSCSI->enmState == VBOXSCSISTATE_READ_BUFFER_SIZE_LSB)
192 {
193 pVBoxSCSI->enmState = VBOXSCSISTATE_READ_BUFFER_SIZE_MID;
194 pVBoxSCSI->cbBuf |= uVal; /* Bits 0-7 of buffer size. */
195 }
196 else if (pVBoxSCSI->enmState == VBOXSCSISTATE_READ_BUFFER_SIZE_MID)
197 {
198 pVBoxSCSI->enmState = VBOXSCSISTATE_READ_COMMAND;
199 pVBoxSCSI->cbBuf |= (((uint16_t)uVal) << 8); /* Bits 8-15 of buffer size. */
200 }
201 else if (pVBoxSCSI->enmState == VBOXSCSISTATE_READ_COMMAND)
202 {
203 pVBoxSCSI->abCDB[pVBoxSCSI->iCDB] = uVal;
204 pVBoxSCSI->iCDB++;
205
206 /* Check if we have all necessary command data. */
207 if (pVBoxSCSI->iCDB == pVBoxSCSI->cbCDB)
208 {
209 Log(("%s: Command ready for processing\n", __FUNCTION__));
210 pVBoxSCSI->enmState = VBOXSCSISTATE_COMMAND_READY;
211 pVBoxSCSI->cbBufLeft = pVBoxSCSI->cbBuf;
212 if (pVBoxSCSI->uTxDir == VBOXSCSI_TXDIR_TO_DEVICE)
213 {
214 /* This is a write allocate buffer. */
215 pVBoxSCSI->pbBuf = (uint8_t *)RTMemAllocZ(pVBoxSCSI->cbBuf);
216 if (!pVBoxSCSI->pbBuf)
217 return VERR_NO_MEMORY;
218 }
219 else
220 {
221 /* This is a read from the device. */
222 ASMAtomicXchgBool(&pVBoxSCSI->fBusy, true);
223 rc = VERR_MORE_DATA; /** @todo Better return value to indicate ready command? */
224 }
225 }
226 }
227 else
228 AssertMsgFailed(("Invalid state %d\n", pVBoxSCSI->enmState));
229 break;
230 }
231
232 case 1:
233 {
234 if ( pVBoxSCSI->enmState != VBOXSCSISTATE_COMMAND_READY
235 || pVBoxSCSI->uTxDir != VBOXSCSI_TXDIR_TO_DEVICE)
236 {
237 /* Reset the state */
238 vboxscsiReset(pVBoxSCSI, true /*fEverything*/);
239 }
240 else if (pVBoxSCSI->cbBufLeft > 0)
241 {
242 pVBoxSCSI->pbBuf[pVBoxSCSI->iBuf++] = uVal;
243 pVBoxSCSI->cbBufLeft--;
244 if (pVBoxSCSI->cbBufLeft == 0)
245 {
246 rc = VERR_MORE_DATA;
247 ASMAtomicXchgBool(&pVBoxSCSI->fBusy, true);
248 }
249 }
250 /* else: Ignore extra data, request pending or something. */
251 break;
252 }
253
254 case 2:
255 {
256 pVBoxSCSI->regIdentify = uVal;
257 break;
258 }
259
260 case 3:
261 {
262 /* Reset */
263 vboxscsiReset(pVBoxSCSI, true /*fEverything*/);
264 break;
265 }
266
267 default:
268 AssertMsgFailed(("Invalid register to write to %u\n", iRegister));
269 }
270
271 return rc;
272}
273
274/**
275 * Sets up a SCSI request which the owning SCSI device can process.
276 *
277 * @returns VBox status code.
278 * @param pVBoxSCSI Pointer to the SCSI state.
279 * @param puLun Where to store the LUN on success.
280 * @param ppbCdb Where to store the pointer to the CDB on success.
281 * @param pcbCdb Where to store the size of the CDB on success.
282 * @param pcbBuf Where to store th size of the data buffer on success.
283 * @param puTargetDevice Where to store the target device ID.
284 */
285int vboxscsiSetupRequest(PVBOXSCSI pVBoxSCSI, uint32_t *puLun, uint8_t **ppbCdb,
286 size_t *pcbCdb, size_t *pcbBuf, uint32_t *puTargetDevice)
287{
288 int rc = VINF_SUCCESS;
289
290 LogFlowFunc(("pVBoxSCSI=%#p puTargetDevice=%#p\n", pVBoxSCSI, puTargetDevice));
291
292 AssertMsg(pVBoxSCSI->enmState == VBOXSCSISTATE_COMMAND_READY, ("Invalid state %u\n", pVBoxSCSI->enmState));
293
294 /* Clear any errors from a previous request. */
295 pVBoxSCSI->rcCompletion = 0;
296
297 if (pVBoxSCSI->uTxDir == VBOXSCSI_TXDIR_FROM_DEVICE)
298 {
299 if (pVBoxSCSI->pbBuf)
300 RTMemFree(pVBoxSCSI->pbBuf);
301
302 pVBoxSCSI->pbBuf = (uint8_t *)RTMemAllocZ(pVBoxSCSI->cbBuf);
303 if (!pVBoxSCSI->pbBuf)
304 return VERR_NO_MEMORY;
305 }
306
307 *puLun = 0;
308 *ppbCdb = &pVBoxSCSI->abCDB[0];
309 *pcbCdb = pVBoxSCSI->cbCDB;
310 *pcbBuf = pVBoxSCSI->cbBuf;
311 *puTargetDevice = pVBoxSCSI->uTargetDevice;
312
313 return rc;
314}
315
316/**
317 * Notifies the device that a request finished and the incoming data
318 * is ready at the incoming data port.
319 */
320int vboxscsiRequestFinished(PVBOXSCSI pVBoxSCSI, int rcCompletion)
321{
322 LogFlowFunc(("pVBoxSCSI=%#p\n", pVBoxSCSI));
323
324 if (pVBoxSCSI->uTxDir == VBOXSCSI_TXDIR_TO_DEVICE)
325 vboxscsiReset(pVBoxSCSI, false /*fEverything*/);
326
327 pVBoxSCSI->rcCompletion = rcCompletion;
328
329 ASMAtomicXchgBool(&pVBoxSCSI->fBusy, false);
330
331 return VINF_SUCCESS;
332}
333
334size_t vboxscsiCopyToBuf(PVBOXSCSI pVBoxSCSI, PRTSGBUF pSgBuf, size_t cbSkip, size_t cbCopy)
335{
336 AssertPtrReturn(pVBoxSCSI->pbBuf, 0);
337 AssertReturn(cbSkip + cbCopy <= pVBoxSCSI->cbBuf, 0);
338
339 void *pvBuf = pVBoxSCSI->pbBuf + cbSkip;
340 return RTSgBufCopyToBuf(pSgBuf, pvBuf, cbCopy);
341}
342
343size_t vboxscsiCopyFromBuf(PVBOXSCSI pVBoxSCSI, PRTSGBUF pSgBuf, size_t cbSkip, size_t cbCopy)
344{
345 AssertPtrReturn(pVBoxSCSI->pbBuf, 0);
346 AssertReturn(cbSkip + cbCopy <= pVBoxSCSI->cbBuf, 0);
347
348 void *pvBuf = pVBoxSCSI->pbBuf + cbSkip;
349 return RTSgBufCopyFromBuf(pSgBuf, pvBuf, cbCopy);
350}
351
352int vboxscsiReadString(PPDMDEVINS pDevIns, PVBOXSCSI pVBoxSCSI, uint8_t iRegister,
353 uint8_t *pbDst, uint32_t *pcTransfers, unsigned cb)
354{
355 RT_NOREF(pDevIns);
356 LogFlowFunc(("pDevIns=%#p pVBoxSCSI=%#p iRegister=%d cTransfers=%u cb=%u\n",
357 pDevIns, pVBoxSCSI, iRegister, *pcTransfers, cb));
358
359 /*
360 * Check preconditions, fall back to non-string I/O handler.
361 */
362 Assert(*pcTransfers > 0);
363
364 /* Read string only valid for data in register. */
365 AssertMsgReturn(iRegister == 1, ("Hey! Only register 1 can be read from with string!\n"), VINF_SUCCESS);
366
367 /* Accesses without a valid buffer will be ignored. */
368 AssertReturn(pVBoxSCSI->pbBuf, VINF_SUCCESS);
369
370 /* Check state. */
371 AssertReturn(pVBoxSCSI->enmState == VBOXSCSISTATE_COMMAND_READY, VINF_SUCCESS);
372 Assert(!pVBoxSCSI->fBusy);
373
374 /*
375 * Also ignore attempts to read more data than is available.
376 */
377 int rc = VINF_SUCCESS;
378 uint32_t cbTransfer = *pcTransfers * cb;
379 if (pVBoxSCSI->cbBufLeft > 0)
380 {
381 Assert(cbTransfer <= pVBoxSCSI->cbBuf);
382 if (cbTransfer > pVBoxSCSI->cbBuf)
383 {
384 memset(pbDst + pVBoxSCSI->cbBuf, 0xff, cbTransfer - pVBoxSCSI->cbBuf);
385 cbTransfer = pVBoxSCSI->cbBuf; /* Ignore excess data (not supposed to happen). */
386 }
387
388 /* Copy the data and adance the buffer position. */
389 memcpy(pbDst, pVBoxSCSI->pbBuf + pVBoxSCSI->iBuf, cbTransfer);
390
391 /* Advance current buffer position. */
392 pVBoxSCSI->iBuf += cbTransfer;
393 pVBoxSCSI->cbBufLeft -= cbTransfer;
394
395 /* When the guest reads the last byte from the data in buffer, clear
396 everything and reset command buffer. */
397 if (pVBoxSCSI->cbBufLeft == 0)
398 vboxscsiReset(pVBoxSCSI, false /*fEverything*/);
399 }
400 else
401 {
402 AssertFailed();
403 memset(pbDst, 0, cbTransfer);
404 }
405 *pcTransfers = 0;
406
407 return rc;
408}
409
410int vboxscsiWriteString(PPDMDEVINS pDevIns, PVBOXSCSI pVBoxSCSI, uint8_t iRegister,
411 uint8_t const *pbSrc, uint32_t *pcTransfers, unsigned cb)
412{
413 RT_NOREF(pDevIns);
414
415 /*
416 * Check preconditions, fall back to non-string I/O handler.
417 */
418 Assert(*pcTransfers > 0);
419 /* Write string only valid for data in/out register. */
420 AssertMsgReturn(iRegister == 1, ("Hey! Only register 1 can be written to with string!\n"), VINF_SUCCESS);
421
422 /* Accesses without a valid buffer will be ignored. */
423 AssertReturn(pVBoxSCSI->pbBuf, VINF_SUCCESS);
424
425 /* State machine assumptions. */
426 AssertReturn(pVBoxSCSI->enmState == VBOXSCSISTATE_COMMAND_READY, VINF_SUCCESS);
427 AssertReturn(pVBoxSCSI->uTxDir == VBOXSCSI_TXDIR_TO_DEVICE, VINF_SUCCESS);
428
429 /*
430 * Ignore excess data (not supposed to happen).
431 */
432 int rc = VINF_SUCCESS;
433 if (pVBoxSCSI->cbBufLeft > 0)
434 {
435 uint32_t cbTransfer = RT_MIN(*pcTransfers * cb, pVBoxSCSI->cbBufLeft);
436
437 /* Copy the data and adance the buffer position. */
438 memcpy(pVBoxSCSI->pbBuf + pVBoxSCSI->iBuf, pbSrc, cbTransfer);
439 pVBoxSCSI->iBuf += cbTransfer;
440 pVBoxSCSI->cbBufLeft -= cbTransfer;
441
442 /* If we've reached the end, tell the caller to submit the command. */
443 if (pVBoxSCSI->cbBufLeft == 0)
444 {
445 ASMAtomicXchgBool(&pVBoxSCSI->fBusy, true);
446 rc = VERR_MORE_DATA;
447 }
448 }
449 else
450 AssertFailed();
451 *pcTransfers = 0;
452
453 return rc;
454}
455
456void vboxscsiSetRequestRedo(PVBOXSCSI pVBoxSCSI)
457{
458 AssertMsg(pVBoxSCSI->fBusy, ("No request to redo\n"));
459
460 if (pVBoxSCSI->uTxDir == VBOXSCSI_TXDIR_FROM_DEVICE)
461 {
462 AssertPtr(pVBoxSCSI->pbBuf);
463 }
464}
465
466DECLHIDDEN(int) vboxscsiR3LoadExec(PVBOXSCSI pVBoxSCSI, PSSMHANDLE pSSM)
467{
468 SSMR3GetU8 (pSSM, &pVBoxSCSI->regIdentify);
469 SSMR3GetU8 (pSSM, &pVBoxSCSI->uTargetDevice);
470 SSMR3GetU8 (pSSM, &pVBoxSCSI->uTxDir);
471 SSMR3GetU8 (pSSM, &pVBoxSCSI->cbCDB);
472
473 /*
474 * The CDB buffer was increased with r104155 in trunk (backported to 5.0
475 * in r104311) without bumping the SSM state versions which leaves us
476 * with broken saved state restoring for older VirtualBox releases
477 * (up to 5.0.10).
478 */
479 if ( ( SSMR3HandleRevision(pSSM) < 104311
480 && SSMR3HandleVersion(pSSM) < VBOX_FULL_VERSION_MAKE(5, 0, 12))
481 || ( SSMR3HandleRevision(pSSM) < 104155
482 && SSMR3HandleVersion(pSSM) >= VBOX_FULL_VERSION_MAKE(5, 0, 51)))
483 {
484 memset(&pVBoxSCSI->abCDB[0], 0, sizeof(pVBoxSCSI->abCDB));
485 SSMR3GetMem (pSSM, &pVBoxSCSI->abCDB[0], 12);
486 }
487 else
488 SSMR3GetMem (pSSM, &pVBoxSCSI->abCDB[0], sizeof(pVBoxSCSI->abCDB));
489
490 SSMR3GetU8 (pSSM, &pVBoxSCSI->iCDB);
491 SSMR3GetU32 (pSSM, &pVBoxSCSI->cbBufLeft);
492 SSMR3GetU32 (pSSM, &pVBoxSCSI->iBuf);
493 SSMR3GetBool(pSSM, (bool *)&pVBoxSCSI->fBusy);
494 SSMR3GetU8 (pSSM, (uint8_t *)&pVBoxSCSI->enmState);
495
496 /*
497 * Old saved states only save the size of the buffer left to read/write.
498 * To avoid changing the saved state version we can just calculate the original
499 * buffer size from the offset and remaining size.
500 */
501 pVBoxSCSI->cbBuf = pVBoxSCSI->cbBufLeft + pVBoxSCSI->iBuf;
502
503 if (pVBoxSCSI->cbBuf)
504 {
505 pVBoxSCSI->pbBuf = (uint8_t *)RTMemAllocZ(pVBoxSCSI->cbBuf);
506 if (!pVBoxSCSI->pbBuf)
507 return VERR_NO_MEMORY;
508
509 SSMR3GetMem(pSSM, pVBoxSCSI->pbBuf, pVBoxSCSI->cbBuf);
510 }
511
512 return VINF_SUCCESS;
513}
514
515DECLHIDDEN(int) vboxscsiR3SaveExec(PVBOXSCSI pVBoxSCSI, PSSMHANDLE pSSM)
516{
517 SSMR3PutU8 (pSSM, pVBoxSCSI->regIdentify);
518 SSMR3PutU8 (pSSM, pVBoxSCSI->uTargetDevice);
519 SSMR3PutU8 (pSSM, pVBoxSCSI->uTxDir);
520 SSMR3PutU8 (pSSM, pVBoxSCSI->cbCDB);
521 SSMR3PutMem (pSSM, pVBoxSCSI->abCDB, sizeof(pVBoxSCSI->abCDB));
522 SSMR3PutU8 (pSSM, pVBoxSCSI->iCDB);
523 SSMR3PutU32 (pSSM, pVBoxSCSI->cbBufLeft);
524 SSMR3PutU32 (pSSM, pVBoxSCSI->iBuf);
525 SSMR3PutBool (pSSM, pVBoxSCSI->fBusy);
526 SSMR3PutU8 (pSSM, pVBoxSCSI->enmState);
527
528 if (pVBoxSCSI->cbBuf)
529 SSMR3PutMem(pSSM, pVBoxSCSI->pbBuf, pVBoxSCSI->cbBuf);
530
531 return VINF_SUCCESS;
532}
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