1 | /* $Id: VSCSISense.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Virtual SCSI driver: Sense handling
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2023 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 | #define LOG_GROUP LOG_GROUP_VSCSI
|
---|
28 | #include <VBox/log.h>
|
---|
29 | #include <iprt/assert.h>
|
---|
30 | #include <iprt/string.h>
|
---|
31 |
|
---|
32 | #include "VSCSIInternal.h"
|
---|
33 |
|
---|
34 | void vscsiSenseInit(PVSCSISENSE pVScsiSense)
|
---|
35 | {
|
---|
36 | memset(pVScsiSense->abSenseBuf, 0, sizeof(pVScsiSense->abSenseBuf));
|
---|
37 |
|
---|
38 | /* Fill in valid sense information (can't be just zeros). */
|
---|
39 | pVScsiSense->abSenseBuf[0] = (1 << 7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
|
---|
40 | pVScsiSense->abSenseBuf[2] = SCSI_SENSE_NONE;
|
---|
41 | pVScsiSense->abSenseBuf[7] = 10;
|
---|
42 | pVScsiSense->abSenseBuf[12] = SCSI_ASC_NONE;
|
---|
43 | }
|
---|
44 |
|
---|
45 | int vscsiReqSenseOkSet(PVSCSISENSE pVScsiSense, PVSCSIREQINT pVScsiReq)
|
---|
46 | {
|
---|
47 | memset(pVScsiSense->abSenseBuf, 0, sizeof(pVScsiSense->abSenseBuf));
|
---|
48 |
|
---|
49 | pVScsiSense->abSenseBuf[0] = (1 << 7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
|
---|
50 | pVScsiSense->abSenseBuf[2] = SCSI_SENSE_NONE;
|
---|
51 | pVScsiSense->abSenseBuf[7] = 10;
|
---|
52 | pVScsiSense->abSenseBuf[12] = SCSI_ASC_NONE;
|
---|
53 | pVScsiSense->abSenseBuf[13] = SCSI_ASC_NONE; /* Should be ASCQ but it has the same value for success. */
|
---|
54 |
|
---|
55 | if (pVScsiReq->pbSense && pVScsiReq->cbSense)
|
---|
56 | {
|
---|
57 | pVScsiReq->cbSenseWritten = RT_MIN(sizeof(pVScsiSense->abSenseBuf), pVScsiReq->cbSense);
|
---|
58 | memcpy(pVScsiReq->pbSense, pVScsiSense->abSenseBuf, pVScsiReq->cbSenseWritten);
|
---|
59 | }
|
---|
60 |
|
---|
61 | return SCSI_STATUS_OK;
|
---|
62 | }
|
---|
63 |
|
---|
64 | int vscsiReqSenseErrorSet(PVSCSISENSE pVScsiSense, PVSCSIREQINT pVScsiReq, uint8_t uSCSISenseKey, uint8_t uSCSIASC, uint8_t uSCSIASCQ)
|
---|
65 | {
|
---|
66 | memset(pVScsiSense->abSenseBuf, 0, sizeof(pVScsiSense->abSenseBuf));
|
---|
67 | pVScsiSense->abSenseBuf[0] = (1 << 7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
|
---|
68 | pVScsiSense->abSenseBuf[2] = uSCSISenseKey;
|
---|
69 | pVScsiSense->abSenseBuf[7] = 10;
|
---|
70 | pVScsiSense->abSenseBuf[12] = uSCSIASC;
|
---|
71 | pVScsiSense->abSenseBuf[13] = uSCSIASCQ;
|
---|
72 |
|
---|
73 | if (pVScsiReq->pbSense && pVScsiReq->cbSense)
|
---|
74 | {
|
---|
75 | pVScsiReq->cbSenseWritten = RT_MIN(sizeof(pVScsiSense->abSenseBuf), pVScsiReq->cbSense);
|
---|
76 | memcpy(pVScsiReq->pbSense, pVScsiSense->abSenseBuf, pVScsiReq->cbSenseWritten);
|
---|
77 | }
|
---|
78 |
|
---|
79 | return SCSI_STATUS_CHECK_CONDITION;
|
---|
80 | }
|
---|
81 |
|
---|
82 | int vscsiReqSenseErrorInfoSet(PVSCSISENSE pVScsiSense, PVSCSIREQINT pVScsiReq, uint8_t uSCSISenseKey, uint8_t uSCSIASC, uint8_t uSCSIASCQ, uint32_t uInfo)
|
---|
83 | {
|
---|
84 | memset(pVScsiSense->abSenseBuf, 0, sizeof(pVScsiSense->abSenseBuf));
|
---|
85 | pVScsiSense->abSenseBuf[0] = RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED; /* Fixed format */
|
---|
86 | pVScsiSense->abSenseBuf[2] = uSCSISenseKey;
|
---|
87 | scsiH2BE_U32(&pVScsiSense->abSenseBuf[3], uInfo);
|
---|
88 | pVScsiSense->abSenseBuf[7] = 10;
|
---|
89 | pVScsiSense->abSenseBuf[12] = uSCSIASC;
|
---|
90 | pVScsiSense->abSenseBuf[13] = uSCSIASCQ;
|
---|
91 |
|
---|
92 | if (pVScsiReq->pbSense && pVScsiReq->cbSense)
|
---|
93 | {
|
---|
94 | pVScsiReq->cbSenseWritten = RT_MIN(sizeof(pVScsiSense->abSenseBuf), pVScsiReq->cbSense);
|
---|
95 | memcpy(pVScsiReq->pbSense, pVScsiSense->abSenseBuf, pVScsiReq->cbSenseWritten);
|
---|
96 | }
|
---|
97 |
|
---|
98 | return SCSI_STATUS_CHECK_CONDITION;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int vscsiReqSenseCmd(PVSCSISENSE pVScsiSense, PVSCSIREQINT pVScsiReq)
|
---|
102 | {
|
---|
103 | /* Copy the current sense data to the buffer. */
|
---|
104 | RTSgBufCopyFromBuf(&pVScsiReq->SgBuf, pVScsiSense->abSenseBuf, sizeof(pVScsiSense->abSenseBuf));
|
---|
105 | return vscsiReqSenseOkSet(pVScsiSense, pVScsiReq);
|
---|
106 | }
|
---|
107 |
|
---|