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