VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/VSCSI/VSCSILunSbc.cpp@ 80541

Last change on this file since 80541 was 80541, checked in by vboxsync, 5 years ago

VSCSI: Fix the amount of transfered data for INQUIRY requests to actually indicate how much was transfered really

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 25.0 KB
Line 
1/* $Id: VSCSILunSbc.cpp 80541 2019-09-02 07:55:16Z vboxsync $ */
2/** @file
3 * Virtual SCSI driver: SBC LUN implementation (hard disks)
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 LOG_GROUP LOG_GROUP_VSCSI
23#include <VBox/log.h>
24#include <iprt/errcore.h>
25#include <VBox/types.h>
26#include <VBox/vscsi.h>
27#include <iprt/cdefs.h>
28#include <iprt/asm.h>
29#include <iprt/assert.h>
30#include <iprt/mem.h>
31#include <iprt/string.h>
32
33#include "VSCSIInternal.h"
34
35/** Maximum of amount of LBAs to unmap with one command. */
36#define VSCSI_UNMAP_LBAS_MAX(a_cbSector) ((10*_1M) / a_cbSector)
37
38/**
39 * SBC LUN instance
40 */
41typedef struct VSCSILUNSBC
42{
43 /** Core LUN structure */
44 VSCSILUNINT Core;
45 /** Sector size of the medium. */
46 uint64_t cbSector;
47 /** Size of the virtual disk. */
48 uint64_t cSectors;
49 /** VPD page pool. */
50 VSCSIVPDPOOL VpdPagePool;
51} VSCSILUNSBC;
52/** Pointer to a SBC LUN instance */
53typedef VSCSILUNSBC *PVSCSILUNSBC;
54
55static DECLCALLBACK(int) vscsiLunSbcInit(PVSCSILUNINT pVScsiLun)
56{
57 PVSCSILUNSBC pVScsiLunSbc = (PVSCSILUNSBC)pVScsiLun;
58 int rc = VINF_SUCCESS;
59 int cVpdPages = 0;
60
61 uint32_t cRegions = vscsiLunMediumGetRegionCount(pVScsiLun);
62 if (cRegions != 1)
63 rc = VERR_INVALID_PARAMETER;
64
65 if (RT_SUCCESS(rc))
66 rc = vscsiLunMediumQueryRegionProperties(pVScsiLun, 0, NULL, &pVScsiLunSbc->cSectors,
67 &pVScsiLunSbc->cbSector, NULL);
68 if (RT_SUCCESS(rc))
69 rc = vscsiVpdPagePoolInit(&pVScsiLunSbc->VpdPagePool);
70
71 /* Create device identification page - mandatory. */
72 if (RT_SUCCESS(rc))
73 {
74 PVSCSIVPDPAGEDEVID pDevIdPage;
75
76 rc = vscsiVpdPagePoolAllocNewPage(&pVScsiLunSbc->VpdPagePool, VSCSI_VPD_DEVID_NUMBER,
77 VSCSI_VPD_DEVID_SIZE, (uint8_t **)&pDevIdPage);
78 if (RT_SUCCESS(rc))
79 {
80 /** @todo Not conforming to the SPC spec but Solaris needs at least a stub to work. */
81 pDevIdPage->u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
82 pDevIdPage->u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
83 pDevIdPage->u16PageLength = RT_H2BE_U16(0x0);
84 cVpdPages++;
85 }
86 }
87
88 if ( RT_SUCCESS(rc)
89 && (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_UNMAP))
90 {
91 PVSCSIVPDPAGEBLOCKLIMITS pBlkPage;
92 PVSCSIVPDPAGEBLOCKPROV pBlkProvPage;
93
94 /* Create the page and fill it. */
95 rc = vscsiVpdPagePoolAllocNewPage(&pVScsiLunSbc->VpdPagePool, VSCSI_VPD_BLOCK_LIMITS_NUMBER,
96 VSCSI_VPD_BLOCK_LIMITS_SIZE, (uint8_t **)&pBlkPage);
97 if (RT_SUCCESS(rc))
98 {
99 pBlkPage->u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
100 pBlkPage->u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
101 pBlkPage->u16PageLength = RT_H2BE_U16(0x3c);
102 pBlkPage->u8MaxCmpWriteLength = 0;
103 pBlkPage->u16OptTrfLengthGran = 0;
104 pBlkPage->u32MaxTrfLength = 0;
105 pBlkPage->u32OptTrfLength = 0;
106 pBlkPage->u32MaxPreXdTrfLength = 0;
107 pBlkPage->u32MaxUnmapLbaCount = RT_H2BE_U32(VSCSI_UNMAP_LBAS_MAX(pVScsiLunSbc->cbSector));
108 pBlkPage->u32MaxUnmapBlkDescCount = UINT32_C(0xffffffff);
109 pBlkPage->u32OptUnmapGranularity = 0;
110 pBlkPage->u32UnmapGranularityAlignment = 0;
111 cVpdPages++;
112 }
113
114 if (RT_SUCCESS(rc))
115 {
116 rc = vscsiVpdPagePoolAllocNewPage(&pVScsiLunSbc->VpdPagePool, VSCSI_VPD_BLOCK_PROV_NUMBER,
117 VSCSI_VPD_BLOCK_PROV_SIZE, (uint8_t **)&pBlkProvPage);
118 if (RT_SUCCESS(rc))
119 {
120 pBlkProvPage->u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
121 pBlkProvPage->u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
122 pBlkProvPage->u16PageLength = RT_H2BE_U16(0x4);
123 pBlkProvPage->u8ThresholdExponent = 1;
124 pBlkProvPage->fLBPU = true;
125 cVpdPages++;
126 }
127 }
128 }
129
130 if ( RT_SUCCESS(rc)
131 && (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_NON_ROTATIONAL))
132 {
133 PVSCSIVPDPAGEBLOCKCHARACTERISTICS pBlkPage;
134
135 /* Create the page and fill it. */
136 rc = vscsiVpdPagePoolAllocNewPage(&pVScsiLunSbc->VpdPagePool, VSCSI_VPD_BLOCK_CHARACTERISTICS_NUMBER,
137 VSCSI_VPD_BLOCK_CHARACTERISTICS_SIZE, (uint8_t **)&pBlkPage);
138 if (RT_SUCCESS(rc))
139 {
140 pBlkPage->u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
141 pBlkPage->u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
142 pBlkPage->u16PageLength = RT_H2BE_U16(0x3c);
143 pBlkPage->u16MediumRotationRate = RT_H2BE_U16(VSCSI_VPD_BLOCK_CHARACT_MEDIUM_ROTATION_RATE_NON_ROTATING);
144 cVpdPages++;
145 }
146 }
147
148 if ( RT_SUCCESS(rc)
149 && cVpdPages)
150 {
151 PVSCSIVPDPAGESUPPORTEDPAGES pVpdPages;
152
153 rc = vscsiVpdPagePoolAllocNewPage(&pVScsiLunSbc->VpdPagePool, VSCSI_VPD_SUPPORTED_PAGES_NUMBER,
154 VSCSI_VPD_SUPPORTED_PAGES_SIZE + cVpdPages, (uint8_t **)&pVpdPages);
155 if (RT_SUCCESS(rc))
156 {
157 unsigned idxVpdPage = 0;
158 pVpdPages->u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
159 pVpdPages->u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
160 pVpdPages->u16PageLength = RT_H2BE_U16(cVpdPages);
161
162 pVpdPages->abVpdPages[idxVpdPage++] = VSCSI_VPD_DEVID_NUMBER;
163
164 if (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_UNMAP)
165 {
166 pVpdPages->abVpdPages[idxVpdPage++] = VSCSI_VPD_BLOCK_LIMITS_NUMBER;
167 pVpdPages->abVpdPages[idxVpdPage++] = VSCSI_VPD_BLOCK_PROV_NUMBER;
168 }
169
170 if (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_NON_ROTATIONAL)
171 pVpdPages->abVpdPages[idxVpdPage++] = VSCSI_VPD_BLOCK_CHARACTERISTICS_NUMBER;
172 }
173 }
174
175 /* For SBC LUNs, there will be no ready state transitions. */
176 pVScsiLunSbc->Core.fReady = true;
177
178 return rc;
179}
180
181static DECLCALLBACK(int) vscsiLunSbcDestroy(PVSCSILUNINT pVScsiLun)
182{
183 PVSCSILUNSBC pVScsiLunSbc = (PVSCSILUNSBC)pVScsiLun;
184
185 vscsiVpdPagePoolDestroy(&pVScsiLunSbc->VpdPagePool);
186
187 return VINF_SUCCESS;
188}
189
190static DECLCALLBACK(int) vscsiLunSbcReqProcess(PVSCSILUNINT pVScsiLun, PVSCSIREQINT pVScsiReq)
191{
192 PVSCSILUNSBC pVScsiLunSbc = (PVSCSILUNSBC)pVScsiLun;
193 int rc = VINF_SUCCESS;
194 int rcReq = SCSI_STATUS_OK;
195 uint64_t uLbaStart = 0;
196 uint32_t cSectorTransfer = 0;
197 VSCSIIOREQTXDIR enmTxDir = VSCSIIOREQTXDIR_INVALID;
198
199 switch(pVScsiReq->pbCDB[0])
200 {
201 case SCSI_INQUIRY:
202 {
203 vscsiReqSetXferSize(pVScsiReq, RT_MIN(sizeof(SCSIINQUIRYDATA), scsiBE2H_U16(&pVScsiReq->pbCDB[3])));
204
205 /* Check for EVPD bit. */
206 if (pVScsiReq->pbCDB[1] & 0x1)
207 {
208 rc = vscsiVpdPagePoolQueryPage(&pVScsiLunSbc->VpdPagePool, pVScsiReq, pVScsiReq->pbCDB[2]);
209 if (RT_UNLIKELY(rc == VERR_NOT_FOUND))
210 {
211 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
212 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
213 rc = VINF_SUCCESS;
214 }
215 else
216 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
217 }
218 else if (pVScsiReq->pbCDB[2] != 0) /* A non zero page code is an error. */
219 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST,
220 SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
221 else
222 {
223 SCSIINQUIRYDATA ScsiInquiryReply;
224
225 memset(&ScsiInquiryReply, 0, sizeof(ScsiInquiryReply));
226
227 ScsiInquiryReply.cbAdditional = 31;
228 ScsiInquiryReply.u5PeripheralDeviceType = SCSI_INQUIRY_DATA_PERIPHERAL_DEVICE_TYPE_DIRECT_ACCESS;
229 ScsiInquiryReply.u3PeripheralQualifier = SCSI_INQUIRY_DATA_PERIPHERAL_QUALIFIER_CONNECTED;
230 ScsiInquiryReply.u3AnsiVersion = 0x05; /* SPC-4 compliant */
231 ScsiInquiryReply.fCmdQue = 1; /* Command queuing supported. */
232 ScsiInquiryReply.fWBus16 = 1;
233
234 const char *pszVendorId = "VBOX";
235 const char *pszProductId = "HARDDISK";
236 const char *pszProductLevel = "1.0";
237 int rcTmp = vscsiLunQueryInqStrings(pVScsiLun, &pszVendorId, &pszProductId, &pszProductLevel);
238 Assert(RT_SUCCESS(rcTmp) || rcTmp == VERR_NOT_FOUND); RT_NOREF(rcTmp);
239
240 scsiPadStrS(ScsiInquiryReply.achVendorId, pszVendorId, 8);
241 scsiPadStrS(ScsiInquiryReply.achProductId, pszProductId, 16);
242 scsiPadStrS(ScsiInquiryReply.achProductLevel, pszProductLevel, 4);
243
244 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, (uint8_t *)&ScsiInquiryReply, sizeof(SCSIINQUIRYDATA));
245 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
246 }
247 break;
248 }
249 case SCSI_READ_CAPACITY:
250 {
251 uint8_t aReply[8];
252 memset(aReply, 0, sizeof(aReply));
253
254 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
255
256 /*
257 * If sector size exceeds the maximum value that is
258 * able to be stored in 4 bytes return 0xffffffff in this field
259 */
260 if (pVScsiLunSbc->cSectors > UINT32_C(0xffffffff))
261 scsiH2BE_U32(aReply, UINT32_C(0xffffffff));
262 else
263 scsiH2BE_U32(aReply, pVScsiLunSbc->cSectors - 1);
264 scsiH2BE_U32(&aReply[4], (uint32_t)pVScsiLunSbc->cbSector);
265 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
266 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
267 break;
268 }
269 case SCSI_MODE_SENSE_6:
270 {
271 uint8_t uModePage = pVScsiReq->pbCDB[2] & 0x3f;
272 uint8_t aReply[24];
273 uint8_t *pu8ReplyPos;
274 bool fValid = false;
275
276 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
277 memset(aReply, 0, sizeof(aReply));
278 aReply[0] = 4; /* Reply length 4. */
279 aReply[1] = 0; /* Default media type. */
280 aReply[2] = RT_BIT(4); /* Caching supported. */
281 aReply[3] = 0; /* Block descriptor length. */
282
283 if (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_READONLY)
284 aReply[2] |= RT_BIT(7); /* Set write protect bit */
285
286 pu8ReplyPos = aReply + 4;
287
288 if ((uModePage == 0x08) || (uModePage == 0x3f))
289 {
290 memset(pu8ReplyPos, 0, 20);
291 *pu8ReplyPos++ = 0x08; /* Page code. */
292 *pu8ReplyPos++ = 0x12; /* Size of the page. */
293 *pu8ReplyPos++ = 0x4; /* Write cache enabled. */
294 fValid = true;
295 } else if (uModePage == 0) {
296 fValid = true;
297 }
298
299 /* Querying unknown pages must fail. */
300 if (fValid) {
301 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
302 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
303 } else {
304 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
305 }
306 break;
307 }
308 case SCSI_MODE_SELECT_6:
309 {
310 uint8_t abParms[12];
311 size_t cbCopied;
312 size_t cbList = pVScsiReq->pbCDB[4];
313
314 vscsiReqSetXferSize(pVScsiReq, pVScsiReq->pbCDB[4]);
315
316 /* Copy the parameters. */
317 cbCopied = RTSgBufCopyToBuf(&pVScsiReq->SgBuf, &abParms[0], sizeof(abParms));
318
319 /* Handle short LOGICAL BLOCK LENGTH parameter. */
320 if ( !(pVScsiReq->pbCDB[1] & 0x01)
321 && cbCopied == sizeof(abParms)
322 && cbList >= 12
323 && abParms[3] == 8)
324 {
325 uint32_t cbBlock;
326
327 cbBlock = scsiBE2H_U24(&abParms[4 + 5]);
328 Log2(("SBC: set LOGICAL BLOCK LENGTH to %u\n", cbBlock));
329 if (cbBlock == 512) /* Fixed block size. */
330 {
331 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
332 break;
333 }
334 }
335 /* Fail any other requests. */
336 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
337 break;
338 }
339 case SCSI_READ_6:
340 {
341 enmTxDir = VSCSIIOREQTXDIR_READ;
342 uLbaStart = ((uint64_t) pVScsiReq->pbCDB[3]
343 | (pVScsiReq->pbCDB[2] << 8)
344 | ((pVScsiReq->pbCDB[1] & 0x1f) << 16));
345 cSectorTransfer = pVScsiReq->pbCDB[4];
346 break;
347 }
348 case SCSI_READ_10:
349 {
350 enmTxDir = VSCSIIOREQTXDIR_READ;
351 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
352 cSectorTransfer = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
353 break;
354 }
355 case SCSI_READ_12:
356 {
357 enmTxDir = VSCSIIOREQTXDIR_READ;
358 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
359 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[6]);
360 break;
361 }
362 case SCSI_READ_16:
363 {
364 enmTxDir = VSCSIIOREQTXDIR_READ;
365 uLbaStart = scsiBE2H_U64(&pVScsiReq->pbCDB[2]);
366 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[10]);
367 break;
368 }
369 case SCSI_WRITE_6:
370 {
371 enmTxDir = VSCSIIOREQTXDIR_WRITE;
372 uLbaStart = ((uint64_t) pVScsiReq->pbCDB[3]
373 | (pVScsiReq->pbCDB[2] << 8)
374 | ((pVScsiReq->pbCDB[1] & 0x1f) << 16));
375 cSectorTransfer = pVScsiReq->pbCDB[4];
376 break;
377 }
378 case SCSI_WRITE_10:
379 {
380 enmTxDir = VSCSIIOREQTXDIR_WRITE;
381 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
382 cSectorTransfer = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
383 break;
384 }
385 case SCSI_WRITE_12:
386 {
387 enmTxDir = VSCSIIOREQTXDIR_WRITE;
388 uLbaStart = scsiBE2H_U32(&pVScsiReq->pbCDB[2]);
389 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[6]);
390 break;
391 }
392 case SCSI_WRITE_16:
393 {
394 enmTxDir = VSCSIIOREQTXDIR_WRITE;
395 uLbaStart = scsiBE2H_U64(&pVScsiReq->pbCDB[2]);
396 cSectorTransfer = scsiBE2H_U32(&pVScsiReq->pbCDB[10]);
397 break;
398 }
399 case SCSI_SYNCHRONIZE_CACHE:
400 {
401 break; /* Handled below */
402 }
403 case SCSI_READ_BUFFER:
404 {
405 uint8_t uDataMode = pVScsiReq->pbCDB[1] & 0x1f;
406
407 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[6]));
408
409 switch (uDataMode)
410 {
411 case 0x00:
412 case 0x01:
413 case 0x02:
414 case 0x03:
415 case 0x0a:
416 break;
417 case 0x0b:
418 {
419 uint8_t aReply[4];
420
421 /* We do not implement an echo buffer. */
422 memset(aReply, 0, sizeof(aReply));
423
424 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
425 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
426 break;
427 }
428 case 0x1a:
429 case 0x1c:
430 break;
431 default:
432 AssertMsgFailed(("Invalid data mode\n"));
433 }
434 break;
435 }
436 case SCSI_VERIFY_10:
437 case SCSI_START_STOP_UNIT:
438 {
439 vscsiReqSetXferSize(pVScsiReq, 0);
440 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
441 break;
442 }
443 case SCSI_LOG_SENSE:
444 {
445 uint8_t uPageCode = pVScsiReq->pbCDB[2] & 0x3f;
446 uint8_t uSubPageCode = pVScsiReq->pbCDB[3];
447
448 vscsiReqSetXferSize(pVScsiReq, scsiBE2H_U16(&pVScsiReq->pbCDB[7]));
449
450 switch (uPageCode)
451 {
452 case 0x00:
453 {
454 if (uSubPageCode == 0)
455 {
456 uint8_t aReply[4];
457
458 aReply[0] = 0;
459 aReply[1] = 0;
460 aReply[2] = 0;
461 aReply[3] = 0;
462 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
463 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
464 break;
465 }
466 }
467 RT_FALL_THRU();
468 default:
469 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
470 }
471 break;
472 }
473 case SCSI_SERVICE_ACTION_IN_16:
474 {
475 switch (pVScsiReq->pbCDB[1] & 0x1f)
476 {
477 case SCSI_SVC_ACTION_IN_READ_CAPACITY_16:
478 {
479 uint8_t aReply[32];
480
481 memset(aReply, 0, sizeof(aReply));
482 scsiH2BE_U64(aReply, pVScsiLunSbc->cSectors - 1);
483 scsiH2BE_U32(&aReply[8], 512);
484 if (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_UNMAP)
485 aReply[14] = 0x80; /* LPME enabled */
486 /* Leave the rest 0 */
487
488 vscsiReqSetXferSize(pVScsiReq, sizeof(aReply));
489 RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, aReply, sizeof(aReply));
490 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
491 break;
492 }
493 default:
494 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00); /* Don't know if this is correct */
495 }
496 break;
497 }
498 case SCSI_UNMAP:
499 {
500 if (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_UNMAP)
501 {
502 uint8_t abHdr[8];
503 size_t cbCopied;
504 size_t cbList = scsiBE2H_U16(&pVScsiReq->pbCDB[7]);
505
506 /* Copy the header. */
507 vscsiReqSetXferSize(pVScsiReq, cbList);
508 cbCopied = RTSgBufCopyToBuf(&pVScsiReq->SgBuf, &abHdr[0], sizeof(abHdr));
509
510 /* Using the anchor bit is not supported. */
511 if ( !(pVScsiReq->pbCDB[1] & 0x01)
512 && cbCopied == sizeof(abHdr)
513 && cbList >= 8)
514 {
515 uint32_t cBlkDesc = scsiBE2H_U16(&abHdr[2]) / 16;
516
517 if (cBlkDesc)
518 {
519 PRTRANGE paRanges = (PRTRANGE)RTMemAllocZ(cBlkDesc * sizeof(RTRANGE));
520 if (paRanges)
521 {
522 for (unsigned i = 0; i < cBlkDesc; i++)
523 {
524 uint8_t abBlkDesc[16];
525
526 cbCopied = RTSgBufCopyToBuf(&pVScsiReq->SgBuf, &abBlkDesc[0], sizeof(abBlkDesc));
527 if (RT_UNLIKELY(cbCopied != sizeof(abBlkDesc)))
528 {
529 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
530 break;
531 }
532
533 paRanges[i].offStart = scsiBE2H_U64(&abBlkDesc[0]) * 512;
534 paRanges[i].cbRange = scsiBE2H_U32(&abBlkDesc[8]) * 512;
535 }
536
537 if (rcReq == SCSI_STATUS_OK)
538 rc = vscsiIoReqUnmapEnqueue(pVScsiLun, pVScsiReq, paRanges, cBlkDesc);
539 if ( rcReq != SCSI_STATUS_OK
540 || RT_FAILURE(rc))
541 RTMemFree(paRanges);
542 }
543 else /* Out of memory. */
544 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_HARDWARE_ERROR, SCSI_ASC_SYSTEM_RESOURCE_FAILURE,
545 SCSI_ASCQ_SYSTEM_BUFFER_FULL);
546 }
547 else /* No block descriptors is not an error condition. */
548 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
549 }
550 else /* Invalid CDB. */
551 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_INV_FIELD_IN_CMD_PACKET, 0x00);
552 }
553 else
554 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE, 0x00);
555
556 break;
557 }
558 default:
559 //AssertMsgFailed(("Command %#x [%s] not implemented\n", pRequest->pbCDB[0], SCSICmdText(pRequest->pbCDB[0])));
560 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_ILLEGAL_OPCODE, 0x00);
561 }
562
563 if (enmTxDir != VSCSIIOREQTXDIR_INVALID)
564 {
565 LogFlow(("%s: uLbaStart=%llu cSectorTransfer=%u\n",
566 __FUNCTION__, uLbaStart, cSectorTransfer));
567
568 vscsiReqSetXferSize(pVScsiReq, cSectorTransfer * 512);
569
570 if (RT_UNLIKELY(uLbaStart + cSectorTransfer > pVScsiLunSbc->cSectors))
571 {
572 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_ILLEGAL_REQUEST, SCSI_ASC_LOGICAL_BLOCK_OOR, 0x00);
573 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
574 }
575 else if (!cSectorTransfer)
576 {
577 /* A 0 transfer length is not an error. */
578 rcReq = vscsiLunReqSenseOkSet(pVScsiLun, pVScsiReq);
579 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
580 }
581 else
582 {
583 /* Enqueue new I/O request */
584 if ( ( enmTxDir == VSCSIIOREQTXDIR_WRITE
585 || enmTxDir == VSCSIIOREQTXDIR_FLUSH)
586 && (pVScsiLun->fFeatures & VSCSI_LUN_FEATURE_READONLY))
587 rcReq = vscsiLunReqSenseErrorSet(pVScsiLun, pVScsiReq, SCSI_SENSE_DATA_PROTECT, SCSI_ASC_WRITE_PROTECTED, 0x00);
588 else
589 rc = vscsiIoReqTransferEnqueue(pVScsiLun, pVScsiReq, enmTxDir,
590 uLbaStart * 512, cSectorTransfer * 512);
591 }
592 }
593 else if (pVScsiReq->pbCDB[0] == SCSI_SYNCHRONIZE_CACHE)
594 {
595 /* Enqueue flush */
596 vscsiReqSetXferSize(pVScsiReq, 0);
597 rc = vscsiIoReqFlushEnqueue(pVScsiLun, pVScsiReq);
598 }
599 else if (pVScsiReq->pbCDB[0] != SCSI_UNMAP) /* Request completed */
600 vscsiDeviceReqComplete(pVScsiLun->pVScsiDevice, pVScsiReq, rcReq, false, VINF_SUCCESS);
601
602 return rc;
603}
604
605VSCSILUNDESC g_VScsiLunTypeSbc =
606{
607 /** enmLunType */
608 VSCSILUNTYPE_SBC,
609 /** pcszDescName */
610 "SBC",
611 /** cbLun */
612 sizeof(VSCSILUNSBC),
613 /** cSupOpcInfo */
614 0,
615 /** paSupOpcInfo */
616 NULL,
617 /** pfnVScsiLunInit */
618 vscsiLunSbcInit,
619 /** pfnVScsiLunDestroy */
620 vscsiLunSbcDestroy,
621 /** pfnVScsiLunReqProcess */
622 vscsiLunSbcReqProcess,
623 /** pfnVScsiLunReqFree */
624 NULL,
625 /** pfnVScsiLunMediumInserted */
626 NULL,
627 /** pfnVScsiLunMediumRemoved */
628 NULL
629};
630
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