VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DrvSCSI.cpp@ 21321

Last change on this file since 21321 was 21321, checked in by vboxsync, 15 years ago

OSE: export SCSI + SATA

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 32.8 KB
Line 
1/* $Id: DrvSCSI.cpp 21321 2009-07-07 12:31:37Z vboxsync $ */
2/** @file
3 *
4 * VBox storage drivers:
5 * Generic SCSI command parser and execution driver
6 */
7
8/*
9 * Copyright (C) 2006-2009 Sun Microsystems, Inc.
10 *
11 * This file is part of VirtualBox Open Source Edition (OSE), as
12 * available from http://www.virtualbox.org. This file is free software;
13 * you can redistribute it and/or modify it under the terms of the GNU
14 * General Public License (GPL) as published by the Free Software
15 * Foundation, in version 2 as it comes in the "COPYING" file of the
16 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
20 * Clara, CA 95054 USA or visit http://www.sun.com if you need
21 * additional information or have any questions.
22 */
23
24/*******************************************************************************
25* Header Files *
26*******************************************************************************/
27//#define DEBUG
28#define LOG_GROUP LOG_GROUP_DRV_SCSI
29#include <VBox/pdmdrv.h>
30#include <VBox/pdmifs.h>
31#include <VBox/pdmthread.h>
32#include <VBox/scsi.h>
33#include <iprt/assert.h>
34#include <iprt/string.h>
35#include <iprt/alloc.h>
36#include <iprt/req.h>
37#include <iprt/semaphore.h>
38
39#include "Builtins.h"
40
41/**
42 * SCSI driver instance data.
43 */
44typedef struct DRVSCSI
45{
46 /** Pointer driver instance. */
47 PPDMDRVINS pDrvIns;
48
49 /** Pointer to the attached driver's base interface. */
50 PPDMIBASE pDrvBase;
51 /** Pointer to the attached driver's block interface. */
52 PPDMIBLOCK pDrvBlock;
53 /** Pointer to the attached driver's async block interface. */
54 PPDMIBLOCKASYNC pDrvBlockAsync;
55 /** Pointer to the attached driver's block bios interface. */
56 PPDMIBLOCKBIOS pDrvBlockBios;
57 /** Pointer to the attached driver's mount interface. */
58 PPDMIMOUNT pDrvMount;
59 /** Pointer to the SCSI port interface of the device above. */
60 PPDMISCSIPORT pDevScsiPort;
61 /** pointer to the Led port interface of the dveice above. */
62 PPDMILEDPORTS pLedPort;
63 /** The scsi connector interface .*/
64 PDMISCSICONNECTOR ISCSIConnector;
65 /** The block port interface. */
66 PDMIBLOCKPORT IPort;
67 /** The optional block async port interface. */
68 PDMIBLOCKASYNCPORT IPortAsync;
69 /** The mount notify interface. */
70 PDMIMOUNTNOTIFY IMountNotify;
71 /** The status LED state for this drive.
72 * used in case the device doesn't has a Led interface
73 * so we can use this to avoid if checks later on. */
74 PDMLED Led;
75 /** pointer to the Led to use. */
76 PPDMLED pLed;
77
78 /** Device type. */
79 PDMBLOCKTYPE enmType;
80 /** BIOS PCHS Geometry. */
81 PDMMEDIAGEOMETRY PCHSGeometry;
82 /** BIOS LCHS Geometry. */
83 PDMMEDIAGEOMETRY LCHSGeometry;
84 /** Number of sectors this device has. */
85 uint64_t cSectors;
86
87 /** The dedicated I/O thread for the non async approach. */
88 PPDMTHREAD pAsyncIOThread;
89 /** Queue for passing the requests to the thread. */
90 PRTREQQUEUE pQueueRequests;
91 /** Release statistics: number of bytes written. */
92 STAMCOUNTER StatBytesWritten;
93 /** Release statistics: number of bytes read. */
94 STAMCOUNTER StatBytesRead;
95} DRVSCSI, *PDRVSCSI;
96
97/** Converts a pointer to DRVSCSI::ISCSIConnecotr to a PDRVSCSI. */
98#define PDMISCSICONNECTOR_2_DRVSCSI(pInterface) ( (PDRVSCSI)((uintptr_t)pInterface - RT_OFFSETOF(DRVSCSI, ISCSIConnector)) )
99
100#ifdef DEBUG
101/**
102 * Dumps a SCSI request structure for debugging purposes.
103 *
104 * @returns nothing.
105 * @param pRequest Pointer to the request to dump.
106 */
107static void drvscsiDumpScsiRequest(PPDMSCSIREQUEST pRequest)
108{
109 Log(("Dump for pRequest=%#p Command: %s\n", pRequest, SCSICmdText(pRequest->pbCDB[0])));
110 Log(("cbCDB=%u\n", pRequest->cbCDB));
111 for (uint32_t i = 0; i < pRequest->cbCDB; i++)
112 Log(("pbCDB[%u]=%#x\n", i, pRequest->pbCDB[i]));
113 Log(("cbScatterGather=%u\n", pRequest->cbScatterGather));
114 Log(("cScatterGatherEntries=%u\n", pRequest->cScatterGatherEntries));
115 /* Print all scatter gather entries. */
116 for (uint32_t i = 0; i < pRequest->cScatterGatherEntries; i++)
117 {
118 Log(("ScatterGatherEntry[%u].cbSeg=%u\n", i, pRequest->paScatterGatherHead[i].cbSeg));
119 Log(("ScatterGatherEntry[%u].pvSeg=%#p\n", i, pRequest->paScatterGatherHead[i].pvSeg));
120 }
121 Log(("pvUser=%#p\n", pRequest->pvUser));
122}
123#endif
124
125/**
126 * Copy the content of a buffer to a scatter gather list only
127 * copying only the amount of data which fits into the
128 * scatter gather list.
129 *
130 * @returns VBox status code.
131 * @param pRequest Pointer to the request which contains the S/G list entries.
132 * @param pvBuf Pointer to the buffer which should be copied.
133 * @param cbBuf Size of the buffer.
134 */
135static int drvscsiScatterGatherListCopyFromBuffer(PPDMSCSIREQUEST pRequest, void *pvBuf, size_t cbBuf)
136{
137 unsigned cSGEntry = 0;
138 PPDMDATASEG pSGEntry = &pRequest->paScatterGatherHead[cSGEntry];
139 uint8_t *pu8Buf = (uint8_t *)pvBuf;
140
141 LogFlowFunc(("pRequest=%#p pvBuf=%#p cbBuf=%u\n", pRequest, pvBuf, cbBuf));
142
143#ifdef DEBUG
144 for (unsigned i = 0; i < cbBuf; i++)
145 Log(("%s: pvBuf[%u]=%#x\n", __FUNCTION__, i, pu8Buf[i]));
146#endif
147
148 while (cSGEntry < pRequest->cScatterGatherEntries)
149 {
150 size_t cbToCopy = (cbBuf < pSGEntry->cbSeg) ? cbBuf : pSGEntry->cbSeg;
151
152 memcpy(pSGEntry->pvSeg, pu8Buf, cbToCopy);
153
154 cbBuf -= cbToCopy;
155 /* We finished. */
156 if (!cbBuf)
157 break;
158
159 /* Advance the buffer. */
160 pu8Buf += cbToCopy;
161
162 /* Go to the next entry in the list. */
163 pSGEntry++;
164 cSGEntry++;
165 }
166
167 return VINF_SUCCESS;
168}
169
170static void drvscsiPadStr(int8_t *pbDst, const char *pbSrc, uint32_t cbSize)
171{
172 for (uint32_t i = 0; i < cbSize; i++)
173 {
174 if (*pbSrc)
175 pbDst[i] = *pbSrc++;
176 else
177 pbDst[i] = ' ';
178 }
179}
180
181/**
182 * Set the sense and advanced sense key in the buffer for error conditions.
183 *
184 * @returns SCSI status code.
185 * @param pRequest Pointer to the request which contains the sense buffer.
186 * @param uSCSISenseKey The sense key to set.
187 * @param uSCSIASC The advanced sense key to set.
188 */
189DECLINLINE(int) drvscsiCmdError(PPDMSCSIREQUEST pRequest, uint8_t uSCSISenseKey, uint8_t uSCSIASC)
190{
191 AssertMsgReturn(pRequest->cbSenseBuffer >= 18, ("Sense buffer is not big enough\n"), SCSI_STATUS_OK);
192 AssertMsgReturn(pRequest->pbSenseBuffer, ("Sense buffer pointer is NULL\n"), SCSI_STATUS_OK);
193 memset(pRequest->pbSenseBuffer, 0, pRequest->cbSenseBuffer);
194 pRequest->pbSenseBuffer[0] = (1 << 7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
195 pRequest->pbSenseBuffer[2] = uSCSISenseKey;
196 pRequest->pbSenseBuffer[7] = 10;
197 pRequest->pbSenseBuffer[12] = uSCSIASC;
198 pRequest->pbSenseBuffer[13] = 0x00; /** @todo: Provide more info. */
199 return SCSI_STATUS_CHECK_CONDITION;
200}
201
202/**
203 * Sets the sense key for a status good condition.
204 *
205 * @returns SCSI status code.
206 * @param pRequest Pointer to the request which contains the sense buffer.
207 */
208DECLINLINE(int) drvscsiCmdOk(PPDMSCSIREQUEST pRequest)
209{
210 AssertMsgReturn(pRequest->cbSenseBuffer >= 18, ("Sense buffer is not big enough\n"), SCSI_STATUS_OK);
211 AssertMsgReturn(pRequest->pbSenseBuffer, ("Sense buffer pointer is NULL\n"), SCSI_STATUS_OK);
212 memset(pRequest->pbSenseBuffer, 0, pRequest->cbSenseBuffer);
213 /*
214 * Setting this breaks Linux guests on the BusLogic controller.
215 * According to the SCSI SPC spec sense data is returned after a
216 * CHECK CONDITION status or a REQUEST SENSE command.
217 * Both SCSI controllers have a feature called Auto Sense which
218 * fetches the sense data automatically from the device
219 * with REQUEST SENSE. So the SCSI subsystem in Linux should
220 * find this sense data even if the command finishes successfully
221 * but if it finds valid sense data it will let the command fail
222 * and it doesn't detect attached disks anymore.
223 * Disabling makes it work again and no other guest shows errors
224 * so I will leave it disabled for now.
225 *
226 * On the other hand it is possible that the devices fetch the sense data
227 * only after a command failed so the content is really invalid if
228 * the command succeeds.
229 */
230#if 0
231 pRequest->pbSenseBuffer[0] = (1 << 7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
232 pRequest->pbSenseBuffer[2] = SCSI_SENSE_NONE;
233 pRequest->pbSenseBuffer[7] = 10;
234 pRequest->pbSenseBuffer[12] = SCSI_ASC_NONE;
235 pRequest->pbSenseBuffer[13] = SCSI_ASC_NONE; /* Should be ASCQ but it has the same value for success. */
236#endif
237 return SCSI_STATUS_OK;
238}
239
240DECLINLINE(void) drvscsiH2BE_U16(uint8_t *pbBuf, uint16_t val)
241{
242 pbBuf[0] = val >> 8;
243 pbBuf[1] = val;
244}
245
246
247DECLINLINE(void) drvscsiH2BE_U24(uint8_t *pbBuf, uint32_t val)
248{
249 pbBuf[0] = val >> 16;
250 pbBuf[1] = val >> 8;
251 pbBuf[2] = val;
252}
253
254
255DECLINLINE(void) drvscsiH2BE_U32(uint8_t *pbBuf, uint32_t val)
256{
257 pbBuf[0] = val >> 24;
258 pbBuf[1] = val >> 16;
259 pbBuf[2] = val >> 8;
260 pbBuf[3] = val;
261}
262
263
264DECLINLINE(uint16_t) drvscsiBE2H_U16(const uint8_t *pbBuf)
265{
266 return (pbBuf[0] << 8) | pbBuf[1];
267}
268
269
270DECLINLINE(uint32_t) drvscsiBE2H_U24(const uint8_t *pbBuf)
271{
272 return (pbBuf[0] << 16) | (pbBuf[1] << 8) | pbBuf[2];
273}
274
275
276DECLINLINE(uint32_t) drvscsiBE2H_U32(const uint8_t *pbBuf)
277{
278 return (pbBuf[0] << 24) | (pbBuf[1] << 16) | (pbBuf[2] << 8) | pbBuf[3];
279}
280
281DECLINLINE(uint64_t) drvscsiBE2H_U64(const uint8_t *pbBuf)
282{
283 return ((uint64_t)pbBuf[0] << 56)
284 | ((uint64_t)pbBuf[1] << 48)
285 | ((uint64_t)pbBuf[2] << 40)
286 | ((uint64_t)pbBuf[3] << 32)
287 | ((uint64_t)pbBuf[4] << 24)
288 | ((uint64_t)pbBuf[5] << 16)
289 | ((uint64_t)pbBuf[6] << 8)
290 | (uint64_t)pbBuf[7];
291}
292
293/**
294 * Parses the CDB of a request and acts accordingly.
295 *
296 * @returns transfer direction type.
297 * @param pThis Pointer to the SCSI driver instance data.
298 * @param pRequest Pointer to the request to process.
299 * @param puOffset Where to store the start offset to start data transfer from.
300 * @param pcbToTransfer Where to store the number of bytes to transfer.
301 * @param piTxDir Where to store the data transfer direction.
302 */
303static int drvscsiProcessCDB(PDRVSCSI pThis, PPDMSCSIREQUEST pRequest, uint64_t *puOffset, uint32_t *pcbToTransfer, int *piTxDir)
304{
305 int iTxDir = PDMBLOCKTXDIR_NONE;
306 int rc = SCSI_STATUS_OK;
307
308 /* We check for a command which needs to be handled even for non existant LUNs. */
309 switch (pRequest->pbCDB[0])
310 {
311 case SCSI_INQUIRY:
312 {
313 SCSIINQUIRYDATA ScsiInquiryReply;
314
315 memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
316
317 ScsiInquiryReply.cbAdditional = 31;
318
319 /* We support only one attached device at LUN0 at the moment. */
320 if (pRequest->uLogicalUnit != 0)
321 {
322 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_UNKNOWN;
323 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_NOT_CONNECTED_NOT_SUPPORTED;
324 }
325 else
326 {
327 switch (pThis->enmType)
328 {
329 case PDMBLOCKTYPE_HARD_DISK:
330 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
331 break;
332 default:
333 AssertMsgFailed(("Device type %u not supported\n", pThis->enmType));
334 }
335
336 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
337 ScsiInquiryReply.u3AnsiVersion = 0x05; /* SPC-4 compliant */
338 drvscsiPadStr(ScsiInquiryReply.achVendorId, "VBOX", 8);
339 drvscsiPadStr(ScsiInquiryReply.achProductId, "HARDDISK", 16);
340 drvscsiPadStr(ScsiInquiryReply.achProductLevel, "1.0", 4);
341 }
342
343 drvscsiScatterGatherListCopyFromBuffer(pRequest, &ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
344 rc = drvscsiCmdOk(pRequest);
345 break;
346 }
347 case SCSI_REPORT_LUNS:
348 {
349 /*
350 * If allocation length is less than 16 bytes SPC compliant devices have
351 * to return an error.
352 */
353 if (drvscsiBE2H_U32(&pRequest->pbCDB[6]) < 16)
354 rc = drvscsiCmdError(pRequest, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET);
355 else
356 {
357 uint8_t aReply[16]; /* We report only one LUN. */
358
359 memset(aReply, 0, sizeof(aReply));
360 drvscsiH2BE_U32(&aReply[0], 8); /* List length starts at position 0. */
361 drvscsiScatterGatherListCopyFromBuffer(pRequest, aReply, sizeof(aReply));
362 rc = drvscsiCmdOk(pRequest);
363 }
364 break;
365 }
366 case SCSI_TEST_UNIT_READY:
367 {
368 rc = drvscsiCmdOk(pRequest);
369 break;
370 }
371 default:
372 {
373 /* Now for commands which are only implemented for existant LUNs. */
374 if (RT_LIKELY(pRequest->uLogicalUnit == 0))
375 {
376 switch(pRequest->pbCDB[0])
377 {
378 case SCSI_READ_CAPACITY:
379 {
380 uint8_t aReply[8];
381 memset(aReply, 0, sizeof(aReply));
382
383 /*
384 * If sector size exceeds the maximum value that is
385 * able to be stored in 4 bytes return 0xffffffff in this field
386 */
387 if (pThis->cSectors > UINT32_C(0xffffffff))
388 drvscsiH2BE_U32(aReply, UINT32_C(0xffffffff));
389 else
390 drvscsiH2BE_U32(aReply, pThis->cSectors - 1);
391 drvscsiH2BE_U32(&aReply[4], 512);
392 drvscsiScatterGatherListCopyFromBuffer(pRequest, aReply, sizeof(aReply));
393 rc = drvscsiCmdOk(pRequest);
394 break;
395 }
396 case SCSI_SERVICE_ACTION_IN_16:
397 {
398 AssertMsgFailed(("Not implemented yet.\n"));
399 break;
400 }
401 case SCSI_MODE_SENSE_6:
402 {
403 uint8_t uModePage = pRequest->pbCDB[2] & 0x3f;
404 uint8_t aReply[24];
405 uint8_t *pu8ReplyPos;
406
407 memset(aReply, 0, sizeof(aReply));
408 aReply[0] = 4; /* Reply length 4. */
409 aReply[1] = 0; /* Default media type. */
410 aReply[2] = RT_BIT(4); /* Caching supported. */
411 aReply[3] = 0; /* Block descriptor length. */
412
413 pu8ReplyPos = aReply + 4;
414
415 if ((uModePage == 0x08) || (uModePage == 0x3f))
416 {
417 memset(pu8ReplyPos, 0, 20);
418 *pu8ReplyPos++ = 0x08; /* Page code. */
419 *pu8ReplyPos++ = 0x12; /* Size of the page. */
420 *pu8ReplyPos++ = 0x4; /* Write cache enabled. */
421 }
422
423 drvscsiScatterGatherListCopyFromBuffer(pRequest, aReply, sizeof(aReply));
424 rc = drvscsiCmdOk(pRequest);
425 break;
426 }
427 case SCSI_READ_6:
428 {
429 iTxDir = PDMBLOCKTXDIR_FROM_DEVICE;
430 *puOffset = ((uint64_t) pRequest->pbCDB[3]
431 | (pRequest->pbCDB[2] << 8)
432 | ((pRequest->pbCDB[1] & 0x1f) << 16)) * 512;
433 *pcbToTransfer = ((uint32_t)pRequest->pbCDB[4]) * 512;
434 break;
435 }
436 case SCSI_READ_10:
437 {
438 iTxDir = PDMBLOCKTXDIR_FROM_DEVICE;
439 *puOffset = ((uint64_t)drvscsiBE2H_U32(&pRequest->pbCDB[2])) * 512;
440 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U16(&pRequest->pbCDB[7])) * 512;
441 break;
442 }
443 case SCSI_READ_12:
444 {
445 iTxDir = PDMBLOCKTXDIR_FROM_DEVICE;
446 *puOffset = ((uint64_t)drvscsiBE2H_U32(&pRequest->pbCDB[2])) * 512;
447 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U32(&pRequest->pbCDB[6])) * 512;
448 break;
449 }
450 case SCSI_READ_16:
451 {
452 iTxDir = PDMBLOCKTXDIR_FROM_DEVICE;
453 *puOffset = drvscsiBE2H_U64(&pRequest->pbCDB[2]) * 512;
454 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U32(&pRequest->pbCDB[6])) * 512;
455 break;
456 }
457 case SCSI_WRITE_6:
458 {
459 iTxDir = PDMBLOCKTXDIR_TO_DEVICE;
460 *puOffset = ((uint64_t) pRequest->pbCDB[3]
461 | (pRequest->pbCDB[2] << 8)
462 | ((pRequest->pbCDB[1] & 0x1f) << 16)) * 512;
463 *pcbToTransfer = ((uint32_t)pRequest->pbCDB[4]) * 512;
464 break;
465 }
466 case SCSI_WRITE_10:
467 {
468 iTxDir = PDMBLOCKTXDIR_TO_DEVICE;
469 *puOffset = ((uint64_t)drvscsiBE2H_U32(&pRequest->pbCDB[2])) * 512;
470 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U16(&pRequest->pbCDB[7])) * 512;
471 break;
472 }
473 case SCSI_WRITE_12:
474 {
475 iTxDir = PDMBLOCKTXDIR_TO_DEVICE;
476 *puOffset = ((uint64_t)drvscsiBE2H_U32(&pRequest->pbCDB[2])) * 512;
477 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U32(&pRequest->pbCDB[6])) * 512;
478 break;
479 }
480 case SCSI_WRITE_16:
481 {
482 iTxDir = PDMBLOCKTXDIR_TO_DEVICE;
483 *puOffset = drvscsiBE2H_U64(&pRequest->pbCDB[2]) * 512;
484 *pcbToTransfer = ((uint32_t)drvscsiBE2H_U32(&pRequest->pbCDB[6])) * 512;
485 break;
486 }
487 case SCSI_SYNCHRONIZE_CACHE:
488 {
489 /* @todo When async mode implemented we have to move this out here. */
490 int rc2 = pThis->pDrvBlock->pfnFlush(pThis->pDrvBlock);
491 AssertMsgRC(rc2, ("Flushing data failed rc=%Rrc\n", rc2));
492 break;
493 }
494 case SCSI_READ_BUFFER:
495 {
496 uint8_t uDataMode = pRequest->pbCDB[1] & 0x1f;
497
498 switch (uDataMode)
499 {
500 case 0x00:
501 case 0x01:
502 case 0x02:
503 case 0x03:
504 case 0x0a:
505 break;
506 case 0x0b:
507 {
508 uint8_t aReply[4];
509
510 /* We do not implement an echo buffer. */
511 memset(aReply, 0, sizeof(aReply));
512
513 drvscsiScatterGatherListCopyFromBuffer(pRequest, aReply, sizeof(aReply));
514 rc = drvscsiCmdOk(pRequest);
515 break;
516 }
517 case 0x1a:
518 case 0x1c:
519 break;
520 default:
521 AssertMsgFailed(("Invalid data mode\n"));
522 }
523 break;
524 }
525 case SCSI_START_STOP_UNIT:
526 {
527 /* Nothing to do. */
528 break;
529 }
530 case SCSI_LOG_SENSE:
531 {
532 uint16_t cbMax = drvscsiBE2H_U16(&pRequest->pbCDB[7]);
533 uint8_t uPageCode = pRequest->pbCDB[2] & 0x3f;
534 uint8_t uSubPageCode = pRequest->pbCDB[3];
535
536 switch (uPageCode)
537 {
538 case 0x00:
539 {
540 if (uSubPageCode == 0)
541 {
542 uint8_t aReply[4];
543
544 aReply[0] = 0;
545 aReply[1] = 0;
546 aReply[2] = 0;
547 aReply[3] = 0;
548 drvscsiScatterGatherListCopyFromBuffer(pRequest, aReply, sizeof(aReply));
549 rc = drvscsiCmdOk(pRequest);
550 break;
551 }
552 }
553 default:
554 rc = drvscsiCmdError(pRequest, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET);
555 }
556 break;
557 }
558 default:
559 //AssertMsgFailed(("Command %#x [%s] not implemented\n", pRequest->pbCDB[0], SCSICmdText(pRequest->pbCDB[0])));
560 rc = drvscsiCmdError(pRequest, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE);
561 }
562 }
563 else
564 {
565 /* Report an error. */
566 rc = drvscsiCmdError(pRequest, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_UNIT_DOES_NOT_RESPOND_TO_SELECTION);
567 }
568 break;
569 }
570 }
571
572 *piTxDir = iTxDir;
573
574 return rc;
575}
576
577static int drvscsiProcessRequestOne(PDRVSCSI pThis, PPDMSCSIREQUEST pRequest)
578{
579 int rc = VINF_SUCCESS;
580 int iTxDir;
581 int rcCompletion;
582 uint64_t uOffset;
583 uint32_t cbToTransfer;
584 uint32_t cSegmentsLeft;
585
586 LogFlowFunc(("Entered\n"));
587
588#ifdef DEBUG
589 drvscsiDumpScsiRequest(pRequest);
590#endif
591 rcCompletion = drvscsiProcessCDB(pThis, pRequest, &uOffset, &cbToTransfer, &iTxDir);
592 if ((rcCompletion == SCSI_STATUS_OK) && (iTxDir != PDMBLOCKTXDIR_NONE))
593 {
594 PPDMDATASEG pSegActual;
595
596 pSegActual = &pRequest->paScatterGatherHead[0];
597 cSegmentsLeft = pRequest->cScatterGatherEntries;
598
599 while(cbToTransfer && cSegmentsLeft)
600 {
601 uint32_t cbProcess = (cbToTransfer < pSegActual->cbSeg) ? cbToTransfer : (uint32_t)pSegActual->cbSeg;
602
603 Log(("%s: uOffset=%llu cbToTransfer=%u\n", __FUNCTION__, uOffset, cbToTransfer));
604
605 if (iTxDir == PDMBLOCKTXDIR_FROM_DEVICE)
606 {
607 pThis->pLed->Asserted.s.fReading = pThis->pLed->Actual.s.fReading = 1;
608 rc = pThis->pDrvBlock->pfnRead(pThis->pDrvBlock, uOffset,
609 pSegActual->pvSeg, cbProcess);
610 pThis->pLed->Actual.s.fReading = 0;
611 if (RT_FAILURE(rc))
612 AssertMsgFailed(("%s: Failed to read data %Rrc\n", __FUNCTION__, rc));
613 STAM_REL_COUNTER_ADD(&pThis->StatBytesRead, cbProcess);
614 }
615 else
616 {
617 pThis->pLed->Asserted.s.fWriting = pThis->pLed->Actual.s.fWriting = 1;
618 rc = pThis->pDrvBlock->pfnWrite(pThis->pDrvBlock, uOffset,
619 pSegActual->pvSeg, cbProcess);
620 pThis->pLed->Actual.s.fWriting = 0;
621 if (RT_FAILURE(rc))
622 AssertMsgFailed(("%s: Failed to write data %Rrc\n", __FUNCTION__, rc));
623 STAM_REL_COUNTER_ADD(&pThis->StatBytesWritten, cbProcess);
624 }
625
626 /* Go to the next entry. */
627 uOffset += cbProcess;
628 cbToTransfer -= cbProcess;
629 pSegActual++;
630 cSegmentsLeft--;
631 }
632 AssertMsg(!cbToTransfer && !cSegmentsLeft,
633 ("Transfer incomplete cbToTransfer=%u cSegmentsLeft=%u", cbToTransfer, cSegmentsLeft));
634 drvscsiCmdOk(pRequest);
635 }
636
637 /* Notify device. */
638 rc = pThis->pDevScsiPort->pfnSCSIRequestCompleted(pThis->pDevScsiPort, pRequest, rcCompletion);
639 AssertMsgRC(rc, ("Error while notifying device rc=%Rrc\n", rc));
640
641 return rc;
642}
643
644/**
645 * Request function to wakeup the thread.
646 *
647 * @returns VWRN_STATE_CHANGED.
648 */
649static int drvscsiAsyncIOLoopWakeupFunc(void)
650{
651 return VWRN_STATE_CHANGED;
652}
653
654/**
655 * The thread function which processes the requests asynchronously.
656 *
657 * @returns VBox status code.
658 * @param pDrvIns Pointer to the device instance data.
659 * @param pThread Pointer to the thread instance data.
660 */
661static int drvscsiAsyncIOLoop(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
662{
663 int rc = VINF_SUCCESS;
664 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
665
666 LogFlowFunc(("Entering async IO loop.\n"));
667
668 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
669 return VINF_SUCCESS;
670
671 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
672 {
673 rc = RTReqProcess(pThis->pQueueRequests, RT_INDEFINITE_WAIT);
674 AssertMsg(rc == VWRN_STATE_CHANGED, ("Left RTReqProcess and error code is not VWRN_STATE_CHANGED rc=%Rrc\n", rc));
675 }
676
677 return VINF_SUCCESS;
678}
679
680static int drvscsiAsyncIOLoopWakeup(PPDMDRVINS pDrvIns, PPDMTHREAD pThread)
681{
682 int rc;
683 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
684 PRTREQ pReq;
685
686 AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
687
688 rc = RTReqCall(pThis->pQueueRequests, &pReq, 10000 /* 10 sec. */, (PFNRT)drvscsiAsyncIOLoopWakeupFunc, 0);
689 AssertMsgRC(rc, ("Inserting request into queue failed rc=%Rrc\n", rc));
690
691 return rc;
692}
693
694/* -=-=-=-=- ISCSIConnector -=-=-=-=- */
695
696/** @copydoc PDMISCSICONNECTOR::pfnSCSIRequestSend. */
697static DECLCALLBACK(int) drvscsiRequestSend(PPDMISCSICONNECTOR pInterface, PPDMSCSIREQUEST pSCSIRequest)
698{
699 int rc;
700 PDRVSCSI pThis = PDMISCSICONNECTOR_2_DRVSCSI(pInterface);
701 PRTREQ pReq;
702
703 AssertMsgReturn(pThis->pQueueRequests, ("pQueueRequests is NULL\n"), VERR_INVALID_STATE);
704
705 rc = RTReqCallEx(pThis->pQueueRequests, &pReq, 0, RTREQFLAGS_NO_WAIT, (PFNRT)drvscsiProcessRequestOne, 2, pThis, pSCSIRequest);
706 AssertMsgReturn(RT_SUCCESS(rc), ("Inserting request into queue failed rc=%Rrc\n", rc), rc);
707
708 return VINF_SUCCESS;
709}
710
711/* -=-=-=-=- IBase -=-=-=-=- */
712
713/** @copydoc PDMIBASE::pfnQueryInterface. */
714static DECLCALLBACK(void *) drvscsiQueryInterface(PPDMIBASE pInterface, PDMINTERFACE enmInterface)
715{
716 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
717 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
718 switch (enmInterface)
719 {
720 case PDMINTERFACE_BASE:
721 return &pDrvIns->IBase;
722 case PDMINTERFACE_SCSI_CONNECTOR:
723 return &pThis->ISCSIConnector;
724 case PDMINTERFACE_BLOCK_PORT:
725 return &pThis->IPort;
726 default:
727 return NULL;
728 }
729}
730
731/**
732 * Destruct a driver instance.
733 *
734 * Most VM resources are freed by the VM. This callback is provided so that any non-VM
735 * resources can be freed correctly.
736 *
737 * @param pDrvIns The driver instance data.
738 */
739static DECLCALLBACK(void) drvscsiDestruct(PPDMDRVINS pDrvIns)
740{
741 int rc;
742 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
743
744 if (pThis->pQueueRequests)
745 {
746 rc = RTReqDestroyQueue(pThis->pQueueRequests);
747 AssertMsgRC(rc, ("Failed to destroy queue rc=%Rrc\n", rc));
748 }
749
750}
751
752/**
753 * Construct a block driver instance.
754 *
755 * @returns VBox status.
756 * @param pDrvIns The driver instance data.
757 * If the registration structure is needed, pDrvIns->pDrvReg points to it.
758 * @param pCfgHandle Configuration node handle for the driver. Use this to obtain the configuration
759 * of the driver instance. It's also found in pDrvIns->pCfgHandle, but like
760 * iInstance it's expected to be used a bit in this function.
761 */
762static DECLCALLBACK(int) drvscsiConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfgHandle)
763{
764 int rc = VINF_SUCCESS;
765 PDMBLOCKTYPE enmType;
766 PDRVSCSI pThis = PDMINS_2_DATA(pDrvIns, PDRVSCSI);
767
768 LogFlowFunc(("pDrvIns=%#p pCfgHandle=%#p\n", pDrvIns, pCfgHandle));
769
770 /*
771 * Initialize interfaces.
772 */
773 pDrvIns->IBase.pfnQueryInterface = drvscsiQueryInterface;
774 pThis->ISCSIConnector.pfnSCSIRequestSend = drvscsiRequestSend;
775
776 /*
777 * Try attach driver below and query it's block interface.
778 */
779 rc = pDrvIns->pDrvHlp->pfnAttach(pDrvIns, &pThis->pDrvBase);
780 AssertMsgReturn(RT_SUCCESS(rc), ("Attaching driver below failed rc=%Rrc\n", rc), rc);
781
782 /*
783 * Query the block and blockbios interfaces.
784 */
785 pThis->pDrvBlock = (PDMIBLOCK *)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_BLOCK);
786 if (!pThis->pDrvBlock)
787 {
788 AssertMsgFailed(("Configuration error: No block interface!\n"));
789 return VERR_PDM_MISSING_INTERFACE;
790 }
791 pThis->pDrvBlockBios = (PDMIBLOCKBIOS *)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_BLOCK_BIOS);
792 if (!pThis->pDrvBlockBios)
793 {
794 AssertMsgFailed(("Configuration error: No block BIOS interface!\n"));
795 return VERR_PDM_MISSING_INTERFACE;
796 }
797
798 /* Query the SCSI port interface above. */
799 pThis->pDevScsiPort = (PPDMISCSIPORT)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_SCSI_PORT);
800 AssertMsgReturn(pThis->pDevScsiPort, ("Missing SCSI port interface above\n"), VERR_PDM_MISSING_INTERFACE);
801
802 pThis->pDrvMount = (PDMIMOUNT *)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_MOUNT);
803
804 /* Query the optional LED interface above. */
805 pThis->pLedPort = (PPDMILEDPORTS)pDrvIns->pUpBase->pfnQueryInterface(pDrvIns->pUpBase, PDMINTERFACE_LED_PORTS);
806 if (pThis->pLedPort != NULL)
807 {
808 /* Get The Led. */
809 rc = pThis->pLedPort->pfnQueryStatusLed(pThis->pLedPort, 0, &pThis->pLed);
810 if (RT_FAILURE(rc))
811 pThis->pLed = &pThis->Led;
812 }
813 else
814 pThis->pLed = &pThis->Led;
815
816 /* Try to get the optional async block interface. */
817 pThis->pDrvBlockAsync = (PDMIBLOCKASYNC *)pThis->pDrvBase->pfnQueryInterface(pThis->pDrvBase, PDMINTERFACE_BLOCK_ASYNC);
818
819 enmType = pThis->pDrvBlock->pfnGetType(pThis->pDrvBlock);
820 if (enmType != PDMBLOCKTYPE_HARD_DISK)
821 {
822 AssertMsgFailed(("Configuration error: Not a disk or cd/dvd-rom. enmType=%d\n", enmType));
823 return VERR_PDM_UNSUPPORTED_BLOCK_TYPE;
824 }
825 pThis->enmType = enmType;
826 pThis->cSectors = pThis->pDrvBlock->pfnGetSize(pThis->pDrvBlock) / 512;
827
828 /* Create request queue. */
829 rc = RTReqCreateQueue(&pThis->pQueueRequests);
830 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create request queue rc=%Rrc\n"), rc);
831
832 /* Register statistics counter. */
833 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesRead, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
834 "Amount of data read.", "/Devices/SCSI/%d/ReadBytes", pDrvIns->iInstance);
835 PDMDrvHlpSTAMRegisterF(pDrvIns, &pThis->StatBytesWritten, STAMTYPE_COUNTER, STAMVISIBILITY_ALWAYS, STAMUNIT_BYTES,
836 "Amount of data written.", "/Devices/SCSI/%d/WrittenBytes", pDrvIns->iInstance);
837
838 /* Create I/O thread. */
839 rc = PDMDrvHlpPDMThreadCreate(pDrvIns, &pThis->pAsyncIOThread, pThis, drvscsiAsyncIOLoop,
840 drvscsiAsyncIOLoopWakeup, 0, RTTHREADTYPE_IO, "SCSI async IO");
841 AssertMsgReturn(RT_SUCCESS(rc), ("Failed to create async I/O thread rc=%Rrc\n"), rc);
842
843 return VINF_SUCCESS;
844}
845
846/**
847 * SCSI driver registration record.
848 */
849const PDMDRVREG g_DrvSCSI =
850{
851 /* u32Version */
852 PDM_DRVREG_VERSION,
853 /* szDriverName */
854 "SCSI",
855 /* pszDescription */
856 "Generic SCSI driver.",
857 /* fFlags */
858 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
859 /* fClass. */
860 PDM_DRVREG_CLASS_SCSI,
861 /* cMaxInstances */
862 ~0,
863 /* cbInstance */
864 sizeof(DRVSCSI),
865 /* pfnConstruct */
866 drvscsiConstruct,
867 /* pfnDestruct */
868 drvscsiDestruct,
869 /* pfnIOCtl */
870 NULL,
871 /* pfnPowerOn */
872 NULL,
873 /* pfnReset */
874 NULL,
875 /* pfnSuspend */
876 NULL,
877 /* pfnResume */
878 NULL,
879 /* pfnDetach */
880 NULL,
881 /* pfnPowerOff */
882 NULL
883};
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