VirtualBox

source: vbox/trunk/src/VBox/Devices/Storage/DevVirtioSCSI.cpp@ 103417

Last change on this file since 103417 was 100400, checked in by vboxsync, 17 months ago

Devices/VirtIO: Add support for the VirtIO over MMIO transport mode useful for ARM, bugref:10459

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 121.1 KB
Line 
1/* $Id: DevVirtioSCSI.cpp 100400 2023-07-06 08:58:02Z vboxsync $ */
2/** @file
3 * VBox storage devices - Virtio SCSI Driver
4 *
5 * Log-levels used:
6 * - Level 1: The most important (but usually rare) things to note
7 * - Level 2: SCSI command logging
8 * - Level 3: Vector and I/O transfer summary (shows what client sent an expects and fulfillment)
9 * - Level 6: Device <-> Guest Driver negotation, traffic, notifications and state handling
10 * - Level 12: Brief formatted hex dumps of I/O data
11 */
12
13/*
14 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
15 *
16 * This file is part of VirtualBox base platform packages, as
17 * available from https://www.virtualbox.org.
18 *
19 * This program is free software; you can redistribute it and/or
20 * modify it under the terms of the GNU General Public License
21 * as published by the Free Software Foundation, in version 3 of the
22 * License.
23 *
24 * This program is distributed in the hope that it will be useful, but
25 * WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
27 * General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, see <https://www.gnu.org/licenses>.
31 *
32 * SPDX-License-Identifier: GPL-3.0-only
33 */
34
35
36/*********************************************************************************************************************************
37* Header Files *
38*********************************************************************************************************************************/
39//#define LOG_GROUP LOG_GROUP_DRV_SCSI
40#define LOG_GROUP LOG_GROUP_DEV_VIRTIO
41
42#include <VBox/vmm/pdmdev.h>
43#include <VBox/vmm/pdmstorageifs.h>
44#include <VBox/vmm/pdmcritsect.h>
45#include <VBox/AssertGuest.h>
46#include <VBox/msi.h>
47#include <VBox/version.h>
48#include <VBox/log.h>
49#include <iprt/errcore.h>
50#include <iprt/assert.h>
51#include <iprt/string.h>
52#include <VBox/sup.h>
53#include "../build/VBoxDD.h"
54#include <VBox/scsi.h>
55#ifdef IN_RING3
56# include <iprt/alloc.h>
57# include <iprt/memcache.h>
58# include <iprt/semaphore.h>
59# include <iprt/sg.h>
60# include <iprt/param.h>
61# include <iprt/uuid.h>
62#endif
63#include "../VirtIO/VirtioCore.h"
64
65#include "VBoxSCSI.h"
66#include "VBoxDD.h"
67
68
69/*********************************************************************************************************************************
70* Defined Constants And Macros *
71*********************************************************************************************************************************/
72/** The current saved state version. */
73#define VIRTIOSCSI_SAVED_STATE_VERSION UINT32_C(1)
74
75
76#define LUN0 0
77/** @name VirtIO 1.0 SCSI Host feature bits (See VirtIO 1.0 specification, Section 5.6.3)
78 * @{ */
79#define VIRTIO_SCSI_F_INOUT RT_BIT_64(0) /** Request is device readable AND writeable */
80#define VIRTIO_SCSI_F_HOTPLUG RT_BIT_64(1) /** Host allows hotplugging SCSI LUNs & targets */
81#define VIRTIO_SCSI_F_CHANGE RT_BIT_64(2) /** Host LUNs chgs via VIRTIOSCSI_T_PARAM_CHANGE evt */
82#define VIRTIO_SCSI_F_T10_PI RT_BIT_64(3) /** Add T10 port info (DIF/DIX) in SCSI req hdr */
83/** @} */
84
85
86#define VIRTIOSCSI_HOST_SCSI_FEATURES_ALL \
87 (VIRTIO_SCSI_F_INOUT | VIRTIO_SCSI_F_HOTPLUG | VIRTIO_SCSI_F_CHANGE | VIRTIO_SCSI_F_T10_PI)
88
89#define VIRTIOSCSI_HOST_SCSI_FEATURES_NONE 0
90
91#define VIRTIOSCSI_HOST_SCSI_FEATURES_OFFERED VIRTIOSCSI_HOST_SCSI_FEATURES_NONE
92
93#define VIRTIOSCSI_REQ_VIRTQ_CNT 4 /**< T.B.D. Consider increasing */
94#define VIRTIOSCSI_VIRTQ_CNT (VIRTIOSCSI_REQ_VIRTQ_CNT + 2)
95#define VIRTIOSCSI_MAX_TARGETS 256 /**< T.B.D. Figure out a a good value for this. */
96#define VIRTIOSCSI_MAX_LUN 1 /**< VirtIO specification, section 5.6.4 */
97#define VIRTIOSCSI_MAX_COMMANDS_PER_LUN 128 /**< T.B.D. What is a good value for this? */
98#define VIRTIOSCSI_MAX_SEG_COUNT 126 /**< T.B.D. What is a good value for this? */
99#define VIRTIOSCSI_MAX_SECTORS_HINT 0x10000 /**< VirtIO specification, section 5.6.4 */
100#define VIRTIOSCSI_MAX_CHANNEL_HINT 0 /**< VirtIO specification, section 5.6.4 should be 0 */
101
102#define PCI_DEVICE_ID_VIRTIOSCSI_HOST 0x1048 /**< Informs guest driver of type of VirtIO device */
103#define PCI_CLASS_BASE_MASS_STORAGE 0x01 /**< PCI Mass Storage device class */
104#define PCI_CLASS_SUB_SCSI_STORAGE_CONTROLLER 0x00 /**< PCI SCSI Controller subclass */
105#define PCI_CLASS_PROG_UNSPECIFIED 0x00 /**< Programming interface. N/A. */
106#define VIRTIOSCSI_PCI_CLASS 0x01 /**< Base class Mass Storage? */
107
108#define VIRTIOSCSI_SENSE_SIZE_DEFAULT 96 /**< VirtIO 1.0: 96 on reset, guest can change */
109#define VIRTIOSCSI_SENSE_SIZE_MAX 4096 /**< Picked out of thin air by bird. */
110#define VIRTIOSCSI_CDB_SIZE_DEFAULT 32 /**< VirtIO 1.0: 32 on reset, guest can change */
111#define VIRTIOSCSI_CDB_SIZE_MAX 255 /**< Picked out of thin air by bird. */
112#define VIRTIOSCSI_PI_BYTES_IN 1 /**< Value TBD (see section 5.6.6.1) */
113#define VIRTIOSCSI_PI_BYTES_OUT 1 /**< Value TBD (see section 5.6.6.1) */
114#define VIRTIOSCSI_DATA_OUT 512 /**< Value TBD (see section 5.6.6.1) */
115
116/**
117 * VirtIO SCSI Host Device device-specific queue indicies.
118 * (Note: # of request queues is determined by virtio_scsi_config.num_queues. VirtIO 1.0, 5.6.4)
119 */
120#define CONTROLQ_IDX 0 /**< VirtIO Spec-defined Index of control queue */
121#define EVENTQ_IDX 1 /**< VirtIO Spec-defined Index of event queue */
122#define VIRTQ_REQ_BASE 2 /**< VirtIO Spec-defined base index of req. queues */
123
124#define VIRTQNAME(uVirtqNbr) (pThis->aszVirtqNames[uVirtqNbr]) /**< Macro to get queue name from its index */
125#define CBVIRTQNAME(uVirtqNbr) RTStrNLen(VIRTQNAME(uVirtqNbr), sizeof(VIRTQNAME(uVirtqNbr)))
126
127#define IS_REQ_VIRTQ(uVirtqNbr) (uVirtqNbr >= VIRTQ_REQ_BASE && uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT)
128
129#define VIRTIO_IS_IN_DIRECTION(pMediaExTxDirEnumValue) \
130 ((pMediaExTxDirEnumValue) == PDMMEDIAEXIOREQSCSITXDIR_FROM_DEVICE)
131
132#define VIRTIO_IS_OUT_DIRECTION(pMediaExTxDirEnumValue) \
133 ((pMediaExTxDirEnumValue) == PDMMEDIAEXIOREQSCSITXDIR_TO_DEVICE)
134
135#define IS_VIRTQ_EMPTY(pDevIns, pVirtio, uVirtqNbr) \
136 (virtioCoreVirtqAvailBufCount(pDevIns, pVirtio, uVirtqNbr) == 0)
137
138
139/*********************************************************************************************************************************
140* Structures and Typedefs *
141*********************************************************************************************************************************/
142/**
143 * VirtIO SCSI Host Device device-specific configuration (see VirtIO 1.0, section 5.6.4)
144 * VBox VirtIO core issues callback to this VirtIO device-specific implementation to handle
145 * MMIO accesses to device-specific configuration parameters.
146 */
147typedef struct virtio_scsi_config
148{
149 uint32_t uNumVirtqs; /**< num_queues \# of req q's exposed by dev */
150 uint32_t uSegMax; /**< seg_max Max \# of segs allowed in cmd */
151 uint32_t uMaxSectors; /**< max_sectors Hint to guest max xfer to use */
152 uint32_t uCmdPerLun; /**< cmd_per_lun Max \# of link cmd sent per lun */
153 uint32_t uEventInfoSize; /**< event_info_size Fill max, evtq bufs */
154 uint32_t uSenseSize; /**< sense_size Max sense data size dev writes */
155 uint32_t uCdbSize; /**< cdb_size Max CDB size driver writes */
156 uint16_t uMaxChannel; /**< max_channel Hint to guest driver */
157 uint16_t uMaxTarget; /**< max_target Hint to guest driver */
158 uint32_t uMaxLun; /**< max_lun Hint to guest driver */
159} VIRTIOSCSI_CONFIG_T, PVIRTIOSCSI_CONFIG_T;
160
161/** @name VirtIO 1.0 SCSI Host Device device specific control types
162 * @{ */
163#define VIRTIOSCSI_T_NO_EVENT 0
164#define VIRTIOSCSI_T_TRANSPORT_RESET 1
165#define VIRTIOSCSI_T_ASYNC_NOTIFY 2 /**< Asynchronous notification */
166#define VIRTIOSCSI_T_PARAM_CHANGE 3
167/** @} */
168
169/**
170 * Device operation: eventq
171 */
172#define VIRTIOSCSI_T_EVENTS_MISSED UINT32_C(0x80000000)
173typedef struct virtio_scsi_event
174{
175 // Device-writable part
176 uint32_t uEvent; /**< event */
177 uint8_t abVirtioLun[8]; /**< lun */
178 uint32_t uReason; /**< reason */
179} VIRTIOSCSI_EVENT_T, *PVIRTIOSCSI_EVENT_T;
180
181/** @name VirtIO 1.0 SCSI Host Device device specific event types
182 * @{ */
183#define VIRTIOSCSI_EVT_RESET_HARD 0 /**< */
184#define VIRTIOSCSI_EVT_RESET_RESCAN 1 /**< */
185#define VIRTIOSCSI_EVT_RESET_REMOVED 2 /**< */
186/** @} */
187
188/**
189 * Device operation: reqestq
190 */
191#pragma pack(1)
192typedef struct REQ_CMD_HDR_T
193{
194 uint8_t abVirtioLun[8]; /**< lun */
195 uint64_t uId; /**< id */
196 uint8_t uTaskAttr; /**< task_attr */
197 uint8_t uPrio; /**< prio */
198 uint8_t uCrn; /**< crn */
199} REQ_CMD_HDR_T;
200#pragma pack()
201AssertCompileSize(REQ_CMD_HDR_T, 19);
202
203typedef struct REQ_CMD_PI_T
204{
205 uint32_t uPiBytesOut; /**< pi_bytesout */
206 uint32_t uPiBytesIn; /**< pi_bytesin */
207} REQ_CMD_PI_T;
208AssertCompileSize(REQ_CMD_PI_T, 8);
209
210typedef struct REQ_RESP_HDR_T
211{
212 uint32_t cbSenseLen; /**< sense_len */
213 uint32_t uResidual; /**< residual */
214 uint16_t uStatusQualifier; /**< status_qualifier */
215 uint8_t uStatus; /**< status SCSI status code */
216 uint8_t uResponse; /**< response */
217} REQ_RESP_HDR_T;
218AssertCompileSize(REQ_RESP_HDR_T, 12);
219
220#pragma pack(1)
221typedef struct VIRTIOSCSI_REQ_CMD_T
222{
223 /** Device-readable section
224 * @{ */
225 REQ_CMD_HDR_T ReqHdr;
226 uint8_t uCdb[1]; /**< cdb */
227
228 REQ_CMD_PI_T piHdr; /**< T10 Pi block integrity (optional feature) */
229 uint8_t uPiOut[1]; /**< pi_out[] T10 pi block integrity */
230 uint8_t uDataOut[1]; /**< dataout */
231 /** @} */
232
233 /** @name Device writable section
234 * @{ */
235 REQ_RESP_HDR_T respHdr;
236 uint8_t uSense[1]; /**< sense */
237 uint8_t uPiIn[1]; /**< pi_in[] T10 Pi block integrity */
238 uint8_t uDataIn[1]; /**< detain; */
239 /** @} */
240} VIRTIOSCSI_REQ_CMD_T, *PVIRTIOSCSI_REQ_CMD_T;
241#pragma pack()
242AssertCompileSize(VIRTIOSCSI_REQ_CMD_T, 19+8+12+6);
243
244/** @name VirtIO 1.0 SCSI Host Device Req command-specific response values
245 * @{ */
246#define VIRTIOSCSI_S_OK 0 /**< control, command */
247#define VIRTIOSCSI_S_OVERRUN 1 /**< control */
248#define VIRTIOSCSI_S_ABORTED 2 /**< control */
249#define VIRTIOSCSI_S_BAD_TARGET 3 /**< control, command */
250#define VIRTIOSCSI_S_RESET 4 /**< control */
251#define VIRTIOSCSI_S_BUSY 5 /**< control, command */
252#define VIRTIOSCSI_S_TRANSPORT_FAILURE 6 /**< control, command */
253#define VIRTIOSCSI_S_TARGET_FAILURE 7 /**< control, command */
254#define VIRTIOSCSI_S_NEXUS_FAILURE 8 /**< control, command */
255#define VIRTIOSCSI_S_FAILURE 9 /**< control, command */
256#define VIRTIOSCSI_S_INCORRECT_LUN 12 /**< command */
257/** @} */
258
259/** @name VirtIO 1.0 SCSI Host Device command-specific task_attr values
260 * @{ */
261#define VIRTIOSCSI_S_SIMPLE 0 /**< */
262#define VIRTIOSCSI_S_ORDERED 1 /**< */
263#define VIRTIOSCSI_S_HEAD 2 /**< */
264#define VIRTIOSCSI_S_ACA 3 /**< */
265/** @} */
266
267/**
268 * VirtIO 1.0 SCSI Host Device Control command before we know type (5.6.6.2)
269 */
270typedef struct VIRTIOSCSI_CTRL_T
271{
272 uint32_t uType;
273} VIRTIOSCSI_CTRL_T, *PVIRTIOSCSI_CTRL_T;
274
275/** @name VirtIO 1.0 SCSI Host Device command-specific TMF values
276 * @{ */
277#define VIRTIOSCSI_T_TMF 0 /**< */
278#define VIRTIOSCSI_T_TMF_ABORT_TASK 0 /**< */
279#define VIRTIOSCSI_T_TMF_ABORT_TASK_SET 1 /**< */
280#define VIRTIOSCSI_T_TMF_CLEAR_ACA 2 /**< */
281#define VIRTIOSCSI_T_TMF_CLEAR_TASK_SET 3 /**< */
282#define VIRTIOSCSI_T_TMF_I_T_NEXUS_RESET 4 /**< */
283#define VIRTIOSCSI_T_TMF_LOGICAL_UNIT_RESET 5 /**< */
284#define VIRTIOSCSI_T_TMF_QUERY_TASK 6 /**< */
285#define VIRTIOSCSI_T_TMF_QUERY_TASK_SET 7 /**< */
286/** @} */
287
288#pragma pack(1)
289typedef struct VIRTIOSCSI_CTRL_TMF_T
290{
291 uint32_t uType; /**< type */
292 uint32_t uSubtype; /**< subtype */
293 uint8_t abScsiLun[8]; /**< lun */
294 uint64_t uId; /**< id */
295} VIRTIOSCSI_CTRL_TMF_T, *PVIRTIOSCSI_CTRL_TMF_T;
296#pragma pack()
297AssertCompileSize(VIRTIOSCSI_CTRL_TMF_T, 24);
298
299/** VirtIO 1.0 section 5.6.6.2, CTRL TMF response is an 8-bit status */
300
301/** @name VirtIO 1.0 SCSI Host Device device specific tmf control response values
302 * @{ */
303#define VIRTIOSCSI_S_FUNCTION_COMPLETE 0 /**< */
304#define VIRTIOSCSI_S_FUNCTION_SUCCEEDED 10 /**< */
305#define VIRTIOSCSI_S_FUNCTION_REJECTED 11 /**< */
306/** @} */
307
308#define VIRTIOSCSI_T_AN_QUERY 1 /**< Asynchronous notification query */
309#define VIRTIOSCSI_T_AN_SUBSCRIBE 2 /**< Asynchronous notification subscription */
310
311#pragma pack(1)
312typedef struct VIRTIOSCSI_CTRL_AN_T
313{
314 uint32_t uType; /**< type */
315 uint8_t abScsiLun[8]; /**< lun */
316 uint32_t fEventsRequested; /**< event_requested */
317} VIRTIOSCSI_CTRL_AN_T, *PVIRTIOSCSI_CTRL_AN_T;
318#pragma pack()
319AssertCompileSize(VIRTIOSCSI_CTRL_AN_T, 16);
320
321/** VirtIO 1.0, Section 5.6.6.2, CTRL AN response is 4-byte evt mask + 8-bit status */
322
323typedef union VIRTIO_SCSI_CTRL_UNION_T
324{
325 VIRTIOSCSI_CTRL_T Type;
326 VIRTIOSCSI_CTRL_TMF_T Tmf;
327 VIRTIOSCSI_CTRL_AN_T AsyncNotify;
328 uint8_t ab[24];
329} VIRTIO_SCSI_CTRL_UNION_T, *PVIRTIO_SCSI_CTRL_UNION_T;
330AssertCompile(sizeof(VIRTIO_SCSI_CTRL_UNION_T) == 24); /* VIRTIOSCSI_CTRL_T forces 4 byte alignment, the other two are byte packed. */
331
332/** @name VirtIO 1.0 SCSI Host Device device specific tmf control response values
333 * @{ */
334#define VIRTIOSCSI_EVT_ASYNC_OPERATIONAL_CHANGE 2 /**< */
335#define VIRTIOSCSI_EVT_ASYNC_POWER_MGMT 4 /**< */
336#define VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST 8 /**< */
337#define VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE 16 /**< */
338#define VIRTIOSCSI_EVT_ASYNC_MULTI_HOST 32 /**< */
339#define VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY 64 /**< */
340/** @} */
341
342#define SUBSCRIBABLE_EVENTS \
343 ( VIRTIOSCSI_EVT_ASYNC_OPERATIONAL_CHANGE \
344 | VIRTIOSCSI_EVT_ASYNC_POWER_MGMT \
345 | VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST \
346 | VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE \
347 | VIRTIOSCSI_EVT_ASYNC_MULTI_HOST \
348 | VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY )
349
350#define SUPPORTED_EVENTS 0 /* TBD */
351
352/**
353 * Worker thread context, shared state.
354 */
355typedef struct VIRTIOSCSIWORKER
356{
357 SUPSEMEVENT hEvtProcess; /**< handle of associated sleep/wake-up semaphore */
358 bool volatile fSleeping; /**< Flags whether worker thread is sleeping or not */
359 bool volatile fNotified; /**< Flags whether worker thread notified */
360} VIRTIOSCSIWORKER;
361/** Pointer to a VirtIO SCSI worker. */
362typedef VIRTIOSCSIWORKER *PVIRTIOSCSIWORKER;
363
364/**
365 * Worker thread context, ring-3 state.
366 */
367typedef struct VIRTIOSCSIWORKERR3
368{
369 R3PTRTYPE(PPDMTHREAD) pThread; /**< pointer to worker thread's handle */
370 RTCRITSECT CritSectVirtq; /**< Protecting the virtq against concurrent thread access. */
371 uint16_t auRedoDescs[VIRTQ_SIZE]; /**< List of previously suspended reqs to re-submit */
372 uint16_t cRedoDescs; /**< Number of redo desc chain head desc idxes in list */
373} VIRTIOSCSIWORKERR3;
374/** Pointer to a VirtIO SCSI worker. */
375typedef VIRTIOSCSIWORKERR3 *PVIRTIOSCSIWORKERR3;
376
377/**
378 * State of a target attached to the VirtIO SCSI Host
379 */
380typedef struct VIRTIOSCSITARGET
381{
382 /** The ring-3 device instance so we can easily get our bearings. */
383 PPDMDEVINSR3 pDevIns;
384
385 /** Pointer to attached driver's base interface. */
386 R3PTRTYPE(PPDMIBASE) pDrvBase;
387
388 /** Target number (PDM LUN) */
389 uint32_t uTarget;
390
391 /** Target Description */
392 R3PTRTYPE(char *) pszTargetName;
393
394 /** Target base interface. */
395 PDMIBASE IBase;
396
397 /** Flag whether device is present. */
398 bool fPresent;
399
400 /** Media port interface. */
401 PDMIMEDIAPORT IMediaPort;
402
403 /** Pointer to the attached driver's media interface. */
404 R3PTRTYPE(PPDMIMEDIA) pDrvMedia;
405
406 /** Extended media port interface. */
407 PDMIMEDIAEXPORT IMediaExPort;
408
409 /** Pointer to the attached driver's extended media interface. */
410 R3PTRTYPE(PPDMIMEDIAEX) pDrvMediaEx;
411
412 /** Status LED interface */
413 PDMILEDPORTS ILed;
414
415 /** The status LED state for this device. */
416 PDMLED led;
417
418} VIRTIOSCSITARGET, *PVIRTIOSCSITARGET;
419
420/**
421 * VirtIO Host SCSI device state, shared edition.
422 *
423 * @extends VIRTIOCORE
424 */
425typedef struct VIRTIOSCSI
426{
427 /** The core virtio state. */
428 VIRTIOCORE Virtio;
429
430 /** VirtIO Host SCSI device runtime configuration parameters */
431 VIRTIOSCSI_CONFIG_T virtioScsiConfig;
432
433 bool fBootable;
434 bool afPadding0[3];
435
436 /** Number of targets in paTargetInstances. */
437 uint32_t cTargets;
438
439 /** Per device-bound virtq worker-thread contexts (eventq slot unused) */
440 VIRTIOSCSIWORKER aWorkers[VIRTIOSCSI_VIRTQ_CNT];
441
442 /** Instance name */
443 char szInstance[16];
444
445 /** Device-specific spec-based VirtIO VIRTQNAMEs */
446 char aszVirtqNames[VIRTIOSCSI_VIRTQ_CNT][VIRTIO_MAX_VIRTQ_NAME_SIZE];
447
448 /** Track which VirtIO queues we've attached to */
449 bool afVirtqAttached[VIRTIOSCSI_VIRTQ_CNT];
450
451 /** Set if events missed due to lack of bufs avail on eventq */
452 bool fEventsMissed;
453
454 /** Explicit alignment padding. */
455 bool afPadding1[2];
456
457 /** Mask of VirtIO Async Event types this device will deliver */
458 uint32_t fAsyncEvtsEnabled;
459
460 /** Total number of requests active across all targets */
461 volatile uint32_t cActiveReqs;
462
463
464 /** True if the guest/driver and VirtIO framework are in the ready state */
465 uint32_t fVirtioReady;
466
467 /** True if VIRTIO_SCSI_F_T10_PI was negotiated */
468 uint32_t fHasT10pi;
469
470 /** True if VIRTIO_SCSI_F_HOTPLUG was negotiated */
471 uint32_t fHasHotplug;
472
473 /** True if VIRTIO_SCSI_F_INOUT was negotiated */
474 uint32_t fHasInOutBufs;
475
476 /** True if VIRTIO_SCSI_F_CHANGE was negotiated */
477 uint32_t fHasLunChange;
478
479 /** True if in the process of resetting */
480 uint32_t fResetting;
481
482} VIRTIOSCSI;
483/** Pointer to the shared state of the VirtIO Host SCSI device. */
484typedef VIRTIOSCSI *PVIRTIOSCSI;
485
486
487/**
488 * VirtIO Host SCSI device state, ring-3 edition.
489 *
490 * @extends VIRTIOCORER3
491 */
492typedef struct VIRTIOSCSIR3
493{
494 /** The core virtio ring-3 state. */
495 VIRTIOCORER3 Virtio;
496
497 /** Array of per-target data. */
498 R3PTRTYPE(PVIRTIOSCSITARGET) paTargetInstances;
499
500 /** Per device-bound virtq worker-thread contexts (eventq slot unused) */
501 VIRTIOSCSIWORKERR3 aWorkers[VIRTIOSCSI_VIRTQ_CNT];
502
503 /** Device base interface. */
504 PDMIBASE IBase;
505
506 /** Pointer to the device instance.
507 * @note Only used in interface callbacks. */
508 PPDMDEVINSR3 pDevIns;
509
510 /** Status Target: LEDs port interface. */
511 PDMILEDPORTS ILeds;
512
513 /** IMediaExPort: Media ejection notification */
514 R3PTRTYPE(PPDMIMEDIANOTIFY) pMediaNotify;
515
516 /** Virtq to send tasks to R3. - HC ptr */
517 R3PTRTYPE(PPDMQUEUE) pNotifierVirtqR3;
518
519 /** True if in the process of quiescing I/O */
520 uint32_t fQuiescing;
521
522 /** For which purpose we're quiescing. */
523 VIRTIOVMSTATECHANGED enmQuiescingFor;
524
525} VIRTIOSCSIR3;
526/** Pointer to the ring-3 state of the VirtIO Host SCSI device. */
527typedef VIRTIOSCSIR3 *PVIRTIOSCSIR3;
528
529
530/**
531 * VirtIO Host SCSI device state, ring-0 edition.
532 */
533typedef struct VIRTIOSCSIR0
534{
535 /** The core virtio ring-0 state. */
536 VIRTIOCORER0 Virtio;
537} VIRTIOSCSIR0;
538/** Pointer to the ring-0 state of the VirtIO Host SCSI device. */
539typedef VIRTIOSCSIR0 *PVIRTIOSCSIR0;
540
541
542/**
543 * VirtIO Host SCSI device state, raw-mode edition.
544 */
545typedef struct VIRTIOSCSIRC
546{
547 /** The core virtio raw-mode state. */
548 VIRTIOCORERC Virtio;
549} VIRTIOSCSIRC;
550/** Pointer to the ring-0 state of the VirtIO Host SCSI device. */
551typedef VIRTIOSCSIRC *PVIRTIOSCSIRC;
552
553
554/** @typedef VIRTIOSCSICC
555 * The instance data for the current context. */
556typedef CTX_SUFF(VIRTIOSCSI) VIRTIOSCSICC;
557/** @typedef PVIRTIOSCSICC
558 * Pointer to the instance data for the current context. */
559typedef CTX_SUFF(PVIRTIOSCSI) PVIRTIOSCSICC;
560
561
562/**
563 * Request structure for IMediaEx (Associated Interfaces implemented by DrvSCSI)
564 * @note cbIn, cbOUt, cbDataOut mostly for debugging
565 */
566typedef struct VIRTIOSCSIREQ
567{
568 PDMMEDIAEXIOREQ hIoReq; /**< Handle of I/O request */
569 PVIRTIOSCSITARGET pTarget; /**< Target */
570 uint16_t uVirtqNbr; /**< Index of queue this request arrived on */
571 PVIRTQBUF pVirtqBuf; /**< Prepared desc chain pulled from virtq avail ring */
572 size_t cbDataIn; /**< size of datain buffer */
573 size_t cbDataOut; /**< size of dataout buffer */
574 uint16_t uDataInOff; /**< Fixed size of respHdr + sense (precede datain) */
575 uint16_t uDataOutOff; /**< Fixed size of reqhdr + cdb (precede dataout) */
576 uint32_t cbSenseAlloc; /**< Size of sense buffer */
577 size_t cbSenseLen; /**< Receives \# bytes written into sense buffer */
578 uint8_t *pbSense; /**< Pointer to R3 sense buffer */
579 PDMMEDIAEXIOREQSCSITXDIR enmTxDir; /**< Receives transfer direction of I/O req */
580 uint8_t uStatus; /**< SCSI status code */
581} VIRTIOSCSIREQ;
582typedef VIRTIOSCSIREQ *PVIRTIOSCSIREQ;
583
584
585/**
586 * callback_method_impl{VIRTIOCORER0,pfnVirtqNotified}
587 * @todo this causes burn if I prefix with at-sign. This callback is in VIRTIOCORER0 and VIRTIOCORER3
588 */
589static DECLCALLBACK(void) virtioScsiNotified(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr)
590{
591
592 RT_NOREF(pVirtio);
593 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
594
595 AssertReturnVoid(uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT);
596 PVIRTIOSCSIWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
597
598#if defined (IN_RING3) && defined (LOG_ENABLED)
599 RTLogFlush(NULL);
600#endif
601
602 if (uVirtqNbr == CONTROLQ_IDX || IS_REQ_VIRTQ(uVirtqNbr))
603 {
604 Log6Func(("%s has available data\n", VIRTQNAME(uVirtqNbr)));
605 /* Wake queue's worker thread up if sleeping */
606 if (!ASMAtomicXchgBool(&pWorker->fNotified, true))
607 {
608 if (ASMAtomicReadBool(&pWorker->fSleeping))
609 {
610 Log6Func(("waking %s worker.\n", VIRTQNAME(uVirtqNbr)));
611 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pWorker->hEvtProcess);
612 AssertRC(rc);
613 }
614 }
615 }
616 else if (uVirtqNbr == EVENTQ_IDX)
617 {
618 Log3Func(("Driver queued buffer(s) to %s\n", VIRTQNAME(uVirtqNbr)));
619// if (ASMAtomicXchgBool(&pThis->fEventsMissed, false))
620// virtioScsiR3ReportEventsMissed(pDevIns, pThis, 0);
621 }
622 else
623 LogFunc(("Unexpected queue idx (ignoring): %d\n", uVirtqNbr));
624}
625
626
627#ifdef IN_RING3 /* spans most of the file, at the moment. */
628
629
630DECLINLINE(void) virtioScsiSetVirtqNames(PVIRTIOSCSI pThis)
631{
632 RTStrCopy(pThis->aszVirtqNames[CONTROLQ_IDX], VIRTIO_MAX_VIRTQ_NAME_SIZE, "controlq");
633 RTStrCopy(pThis->aszVirtqNames[EVENTQ_IDX], VIRTIO_MAX_VIRTQ_NAME_SIZE, "eventq");
634 for (uint16_t uVirtqNbr = VIRTQ_REQ_BASE; uVirtqNbr < VIRTQ_REQ_BASE + VIRTIOSCSI_REQ_VIRTQ_CNT; uVirtqNbr++)
635 RTStrPrintf(pThis->aszVirtqNames[uVirtqNbr], VIRTIO_MAX_VIRTQ_NAME_SIZE,
636 "requestq<%d>", uVirtqNbr - VIRTQ_REQ_BASE);
637}
638
639#ifdef LOG_ENABLED
640
641
642DECLINLINE(const char *) virtioGetTxDirText(uint32_t enmTxDir)
643{
644 switch (enmTxDir)
645 {
646 case PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN: return "<UNKNOWN>";
647 case PDMMEDIAEXIOREQSCSITXDIR_FROM_DEVICE: return "<DEV-TO-GUEST>";
648 case PDMMEDIAEXIOREQSCSITXDIR_TO_DEVICE: return "<GUEST-TO-DEV>";
649 case PDMMEDIAEXIOREQSCSITXDIR_NONE: return "<NONE>";
650 default: return "<BAD ENUM>";
651 }
652}
653
654DECLINLINE(const char *) virtioGetTMFTypeText(uint32_t uSubType)
655{
656 switch (uSubType)
657 {
658 case VIRTIOSCSI_T_TMF_ABORT_TASK: return "ABORT TASK";
659 case VIRTIOSCSI_T_TMF_ABORT_TASK_SET: return "ABORT TASK SET";
660 case VIRTIOSCSI_T_TMF_CLEAR_ACA: return "CLEAR ACA";
661 case VIRTIOSCSI_T_TMF_CLEAR_TASK_SET: return "CLEAR TASK SET";
662 case VIRTIOSCSI_T_TMF_I_T_NEXUS_RESET: return "I T NEXUS RESET";
663 case VIRTIOSCSI_T_TMF_LOGICAL_UNIT_RESET: return "LOGICAL UNIT RESET";
664 case VIRTIOSCSI_T_TMF_QUERY_TASK: return "QUERY TASK";
665 case VIRTIOSCSI_T_TMF_QUERY_TASK_SET: return "QUERY TASK SET";
666 default: return "<unknown>";
667 }
668}
669
670DECLINLINE(const char *) virtioGetReqRespText(uint32_t vboxRc)
671{
672 switch (vboxRc)
673 {
674 case VIRTIOSCSI_S_OK: return "OK/COMPLETE";
675 case VIRTIOSCSI_S_OVERRUN: return "OVERRRUN";
676 case VIRTIOSCSI_S_ABORTED: return "ABORTED";
677 case VIRTIOSCSI_S_BAD_TARGET: return "BAD TARGET";
678 case VIRTIOSCSI_S_RESET: return "RESET";
679 case VIRTIOSCSI_S_TRANSPORT_FAILURE: return "TRANSPORT FAILURE";
680 case VIRTIOSCSI_S_TARGET_FAILURE: return "TARGET FAILURE";
681 case VIRTIOSCSI_S_NEXUS_FAILURE: return "NEXUS FAILURE";
682 case VIRTIOSCSI_S_BUSY: return "BUSY";
683 case VIRTIOSCSI_S_FAILURE: return "FAILURE";
684 case VIRTIOSCSI_S_INCORRECT_LUN: return "INCORRECT LUN";
685 case VIRTIOSCSI_S_FUNCTION_SUCCEEDED: return "FUNCTION SUCCEEDED";
686 case VIRTIOSCSI_S_FUNCTION_REJECTED: return "FUNCTION REJECTED";
687 default: return "<unknown>";
688 }
689}
690
691DECLINLINE(void) virtioGetControlAsyncMaskText(char *pszOutput, uint32_t cbOutput, uint32_t fAsyncTypesMask)
692{
693 RTStrPrintf(pszOutput, cbOutput, "%s%s%s%s%s%s",
694 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_OPERATIONAL_CHANGE ? "CHANGE_OPERATION " : "",
695 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_POWER_MGMT ? "POWER_MGMT " : "",
696 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_EXTERNAL_REQUEST ? "EXTERNAL_REQ " : "",
697 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE ? "MEDIA_CHANGE " : "",
698 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_MULTI_HOST ? "MULTI_HOST " : "",
699 fAsyncTypesMask & VIRTIOSCSI_EVT_ASYNC_DEVICE_BUSY ? "DEVICE_BUSY " : "");
700}
701
702static uint8_t virtioScsiEstimateCdbLen(uint8_t uCmd, uint8_t cbMax)
703{
704 if (uCmd < 0x1f)
705 return RT_MIN(6, cbMax);
706 if (uCmd >= 0x20 && uCmd < 0x60)
707 return RT_MIN(10, cbMax);
708 if (uCmd >= 0x60 && uCmd < 0x80)
709 return cbMax;
710 if (uCmd >= 0x80 && uCmd < 0xa0)
711 return RT_MIN(16, cbMax);
712 if (uCmd >= 0xa0 && uCmd < 0xc0)
713 return RT_MIN(12, cbMax);
714 return cbMax;
715}
716
717#endif /* LOG_ENABLED */
718
719
720/**
721 * Wrapper around virtioCoreR3VirtqUsedBufPut() and virtioCoreVirtqUsedRingSync() doing some device locking.
722 *
723 * @param pDevIns The PDM device instance.
724 * @param pVirtio Pointer to the shared virtio core structure.
725 * @param uVirtqNbr The virtq number.
726 * @param pSgVirtReturn The S/G buffer to return data from.
727 * @param pVirtqBuf The virtq buffer.
728 *
729 * @note This is a temporary fix, see @bugref{9440#c164} on why it is necessary. Should be moved to VirtioCore or
730 * VirtioCore should be changed to not require locking.
731 */
732DECLINLINE(void) virtioScsiR3VirtqUsedBufPutAndSync(PPDMDEVINS pDevIns, PVIRTIOCORE pVirtio, uint16_t uVirtqNbr, PRTSGBUF pSgVirtReturn,
733 PVIRTQBUF pVirtqBuf)
734{
735 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
736
737 RTCritSectEnter(&pThisCC->aWorkers[uVirtqNbr].CritSectVirtq);
738 virtioCoreR3VirtqUsedBufPut(pThisCC->pDevIns, pVirtio, uVirtqNbr, pSgVirtReturn, pVirtqBuf, true /* fFence */);
739 virtioCoreVirtqUsedRingSync(pThisCC->pDevIns, pVirtio, uVirtqNbr);
740 RTCritSectLeave(&pThisCC->aWorkers[uVirtqNbr].CritSectVirtq);
741}
742
743
744/*
745 * @todo Figure out how to implement this with R0 changes. Not used by current linux driver
746 */
747
748#if 0
749static int virtioScsiR3SendEvent(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget, uint32_t uEventType, uint32_t uReason)
750{
751 switch (uEventType)
752 {
753 case VIRTIOSCSI_T_NO_EVENT:
754 Log6Func(("(target=%d, LUN=%d): Warning event info guest queued is shorter than configured\n", uTarget, LUN0));
755 break;
756 case VIRTIOSCSI_T_NO_EVENT | VIRTIOSCSI_T_EVENTS_MISSED:
757 Log6Func(("(target=%d, LUN=%d): Warning driver that events were missed\n", uTarget, LUN0));
758 break;
759 case VIRTIOSCSI_T_TRANSPORT_RESET:
760 switch (uReason)
761 {
762 case VIRTIOSCSI_EVT_RESET_REMOVED:
763 Log6Func(("(target=%d, LUN=%d): Target or LUN removed\n", uTarget, LUN0));
764 break;
765 case VIRTIOSCSI_EVT_RESET_RESCAN:
766 Log6Func(("(target=%d, LUN=%d): Target or LUN added\n", uTarget, LUN0));
767 break;
768 case VIRTIOSCSI_EVT_RESET_HARD:
769 Log6Func(("(target=%d, LUN=%d): Target was reset\n", uTarget, LUN0));
770 break;
771 }
772 break;
773 case VIRTIOSCSI_T_ASYNC_NOTIFY:
774 {
775#ifdef LOG_ENABLED
776 char szTypeText[128];
777 virtioGetControlAsyncMaskText(szTypeText, sizeof(szTypeText), uReason);
778 Log6Func(("(target=%d, LUN=%d): Delivering subscribed async notification %s\n", uTarget, LUN0, szTypeText));
779#endif
780 break;
781 }
782 case VIRTIOSCSI_T_PARAM_CHANGE:
783 LogFunc(("(target=%d, LUN=%d): PARAM_CHANGE sense code: 0x%x sense qualifier: 0x%x\n",
784 uTarget, LUN0, uReason & 0xff, (uReason >> 8) & 0xff));
785 break;
786 default:
787 Log6Func(("(target=%d, LUN=%d): Unknown event type: %d, ignoring\n", uTarget, LUN0, uEventType));
788 return VINF_SUCCESS;
789 }
790
791 PVIRTQBUF pVirtqBuf = NULL;
792 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, EVENTQ_IDX, &pVirtqBuf, true);
793 if (rc == VERR_NOT_AVAILABLE)
794 {
795 LogFunc(("eventq is empty, events missed (driver didn't preload queue)!\n"));
796 ASMAtomicWriteBool(&pThis->fEventsMissed, true);
797 return VINF_SUCCESS;
798 }
799 AssertRCReturn(rc, rc);
800
801 VIRTIOSCSI_EVENT_T Event;
802 Event.uEvent = uEventType;
803 Event.uReason = uReason;
804 Event.abVirtioLun[0] = 1;
805 Event.abVirtioLun[1] = uTarget;
806 Event.abVirtioLun[2] = (LUN0 >> 8) & 0x40;
807 Event.abVirtioLun[3] = LUN0 & 0xff;
808 Event.abVirtioLun[4] = 0;
809 Event.abVirtioLun[5] = 0;
810 Event.abVirtioLun[6] = 0;
811 Event.abVirtioLun[7] = 0;
812
813 RTSGSEG aReqSegs[1];
814 aReqSegs[0].pvSeg = &Event;
815 aReqSegs[0].cbSeg = sizeof(Event);
816
817 RTSGBUF ReqSgBuf;
818 RTSgBufInit(&ReqSgBuf, aReqSegs, RT_ELEMENTS(aReqSegs));
819
820 virtioScsiR3VirtqUsedBufPutAndSync(pDevIns, &pThis->Virtio, EVENTQ_IDX, &ReqSgBuf, pVirtqBuf);
821 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
822
823 return rc;
824}
825#endif
826
827/**
828 * Releases one reference from the given controller instances active request counter.
829 *
830 * @param pDevIns The device instance.
831 * @param pThis VirtIO SCSI shared instance data.
832 * @param pThisCC VirtIO SCSI ring-3 instance data.
833 */
834DECLINLINE(void) virtioScsiR3Release(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, PVIRTIOSCSICC pThisCC)
835{
836 Assert(pThis->cActiveReqs);
837
838 if (!ASMAtomicDecU32(&pThis->cActiveReqs) && pThisCC->fQuiescing)
839 PDMDevHlpAsyncNotificationCompleted(pDevIns);
840}
841
842/**
843 * Retains one reference for the given controller instances active request counter.
844 *
845 * @param pThis VirtIO SCSI shared instance data.
846 */
847DECLINLINE(void) virtioScsiR3Retain(PVIRTIOSCSI pThis)
848{
849 ASMAtomicIncU32(&pThis->cActiveReqs);
850}
851
852/** Internal worker. */
853static void virtioScsiR3FreeReq(PVIRTIOSCSITARGET pTarget, PVIRTIOSCSIREQ pReq)
854{
855 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pTarget->pDevIns, PVIRTIOSCSI);
856 RTMemFree(pReq->pbSense);
857 pReq->pbSense = NULL;
858 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pReq->pVirtqBuf);
859 pReq->pVirtqBuf = NULL;
860 pTarget->pDrvMediaEx->pfnIoReqFree(pTarget->pDrvMediaEx, pReq->hIoReq);
861}
862
863/**
864 * This is called to complete a request immediately
865 *
866 * @param pDevIns The device instance.
867 * @param pThis VirtIO SCSI shared instance data.
868 * @param uVirtqNbr Virtq index
869 * @param pVirtqBuf Pointer to pre-processed descriptor chain pulled from virtq
870 * @param pRespHdr Response header
871 * @param pbSense Pointer to sense buffer or NULL if none.
872 * @param cbSenseCfg The configured sense buffer size.
873 *
874 * @returns VINF_SUCCESS
875 */
876static int virtioScsiR3ReqErr(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uVirtqNbr,
877 PVIRTQBUF pVirtqBuf, REQ_RESP_HDR_T *pRespHdr, uint8_t *pbSense,
878 size_t cbSenseCfg)
879{
880 Log2Func((" status: %s response: %s\n",
881 SCSIStatusText(pRespHdr->uStatus), virtioGetReqRespText(pRespHdr->uResponse)));
882
883 RTSGSEG aReqSegs[2];
884
885 /* Segment #1: Response header*/
886 aReqSegs[0].pvSeg = pRespHdr;
887 aReqSegs[0].cbSeg = sizeof(*pRespHdr);
888
889 /* Segment #2: Sense data. */
890 uint8_t abSenseBuf[VIRTIOSCSI_SENSE_SIZE_MAX];
891 AssertCompile(VIRTIOSCSI_SENSE_SIZE_MAX <= 4096);
892 Assert(cbSenseCfg <= sizeof(abSenseBuf));
893
894 RT_ZERO(abSenseBuf);
895 if (pbSense && pRespHdr->cbSenseLen)
896 memcpy(abSenseBuf, pbSense, RT_MIN(pRespHdr->cbSenseLen, sizeof(abSenseBuf)));
897 else
898 pRespHdr->cbSenseLen = 0;
899
900 aReqSegs[1].pvSeg = abSenseBuf;
901 aReqSegs[1].cbSeg = cbSenseCfg;
902
903 /* Init S/G buffer. */
904 RTSGBUF ReqSgBuf;
905 RTSgBufInit(&ReqSgBuf, aReqSegs, RT_ELEMENTS(aReqSegs));
906
907 if (pThis->fResetting)
908 pRespHdr->uResponse = VIRTIOSCSI_S_RESET;
909
910 virtioScsiR3VirtqUsedBufPutAndSync(pDevIns, &pThis->Virtio, uVirtqNbr, &ReqSgBuf, pVirtqBuf);
911 Log2(("---------------------------------------------------------------------------------\n"));
912
913 return VINF_SUCCESS;
914}
915
916
917/**
918 * Variant of virtioScsiR3ReqErr that takes four (4) REQ_RESP_HDR_T member
919 * fields rather than a pointer to an initialized structure.
920 *
921 * @param pDevIns The device instance.
922 * @param pThis VirtIO SCSI shared instance data.
923 * @param uVirtqNbr Virtq index
924 * @param pVirtqBuf Pointer to pre-processed descriptor chain pulled from virtq
925 * @param cbResidual The number of residual bytes or something like that.
926 * @param bStatus The SCSI status code.
927 * @param bResponse The virtio SCSI response code.
928 * @param pbSense Pointer to sense buffer or NULL if none.
929 * @param cbSense The number of bytes of sense data. Zero if none.
930 * @param cbSenseCfg The configured sense buffer size.
931 *
932 * @returns VINF_SUCCESS
933 */
934static int virtioScsiR3ReqErr4(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uVirtqNbr,
935 PVIRTQBUF pVirtqBuf, size_t cbResidual, uint8_t bStatus, uint8_t bResponse,
936 uint8_t *pbSense, size_t cbSense, size_t cbSenseCfg)
937{
938 REQ_RESP_HDR_T RespHdr;
939 RespHdr.cbSenseLen = cbSense & UINT32_MAX;
940 RespHdr.uResidual = cbResidual & UINT32_MAX;
941 RespHdr.uStatusQualifier = 0;
942 RespHdr.uStatus = bStatus;
943 RespHdr.uResponse = bResponse;
944
945 return virtioScsiR3ReqErr(pDevIns, pThis, uVirtqNbr, pVirtqBuf, &RespHdr, pbSense, cbSenseCfg);
946}
947
948static void virtioScsiR3SenseKeyToVirtioResp(REQ_RESP_HDR_T *respHdr, uint8_t uSenseKey)
949{
950 switch (uSenseKey)
951 {
952 case SCSI_SENSE_ABORTED_COMMAND:
953 respHdr->uResponse = VIRTIOSCSI_S_ABORTED;
954 break;
955 case SCSI_SENSE_COPY_ABORTED:
956 respHdr->uResponse = VIRTIOSCSI_S_ABORTED;
957 break;
958 case SCSI_SENSE_UNIT_ATTENTION:
959 respHdr->uResponse = VIRTIOSCSI_S_TARGET_FAILURE;
960 break;
961 case SCSI_SENSE_HARDWARE_ERROR:
962 respHdr->uResponse = VIRTIOSCSI_S_TARGET_FAILURE;
963 break;
964 case SCSI_SENSE_NOT_READY:
965 /* Not sure what to return for this. See choices at VirtIO 1.0, 5.6.6.1.1 */
966 respHdr->uResponse = VIRTIOSCSI_S_FAILURE;
967 /* respHdr->uResponse = VIRTIOSCSI_S_BUSY; */ /* BUSY is VirtIO's 'retryable' response */
968 break;
969 default:
970 respHdr->uResponse = VIRTIOSCSI_S_FAILURE;
971 break;
972 }
973}
974
975/**
976 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCompleteNotify}
977 */
978static DECLCALLBACK(int) virtioScsiR3IoReqFinish(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
979 void *pvIoReqAlloc, int rcReq)
980{
981 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaExPort);
982 PPDMDEVINS pDevIns = pTarget->pDevIns;
983 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
984 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
985 PPDMIMEDIAEX pIMediaEx = pTarget->pDrvMediaEx;
986 PVIRTIOSCSIREQ pReq = (PVIRTIOSCSIREQ)pvIoReqAlloc;
987
988 size_t cbResidual = 0;
989 int rc = pIMediaEx->pfnIoReqQueryResidual(pIMediaEx, hIoReq, &cbResidual);
990 AssertRC(rc);
991
992 size_t cbXfer = 0;
993 rc = pIMediaEx->pfnIoReqQueryXferSize(pIMediaEx, hIoReq, &cbXfer);
994 AssertRC(rc);
995
996 /* Masking deals with data type size discrepancies between
997 * The APIs (virtio and VBox). Windows C-compiler complains otherwise */
998 Assert(!(cbXfer & 0xffffffff00000000));
999 uint32_t cbXfer32 = cbXfer & 0xffffffff;
1000 REQ_RESP_HDR_T respHdr = { 0 };
1001 respHdr.cbSenseLen = pReq->pbSense[2] == SCSI_SENSE_NONE ? 0 : (uint32_t)pReq->cbSenseLen;
1002 AssertMsg(!(cbResidual & 0xffffffff00000000),
1003 ("WARNING: Residual size larger than sizeof(uint32_t), truncating"));
1004 respHdr.uResidual = (uint32_t)(cbResidual & 0xffffffff);
1005 respHdr.uStatus = pReq->uStatus;
1006
1007 /* VirtIO 1.0 spec 5.6.6.1.1 says device MUST return a VirtIO response byte value.
1008 * Some are returned during the submit phase, and a few are not mapped at all,
1009 * wherein anything that can't map specifically gets mapped to VIRTIOSCSI_S_FAILURE
1010 */
1011 if (pThis->fResetting)
1012 respHdr.uResponse = VIRTIOSCSI_S_RESET;
1013 else
1014 {
1015 switch (rcReq)
1016 {
1017 case SCSI_STATUS_OK:
1018 {
1019 if (pReq->uStatus != SCSI_STATUS_CHECK_CONDITION)
1020 respHdr.uResponse = VIRTIOSCSI_S_OK;
1021 else
1022 virtioScsiR3SenseKeyToVirtioResp(&respHdr, pReq->pbSense[2]);
1023 break;
1024 }
1025 case SCSI_STATUS_CHECK_CONDITION:
1026 virtioScsiR3SenseKeyToVirtioResp(&respHdr, pReq->pbSense[2]);
1027 break;
1028
1029 default:
1030 respHdr.uResponse = VIRTIOSCSI_S_FAILURE;
1031 break;
1032 }
1033 }
1034
1035 Log2Func(("status: (%d) %s, response: (%d) %s\n", pReq->uStatus, SCSIStatusText(pReq->uStatus),
1036 respHdr.uResponse, virtioGetReqRespText(respHdr.uResponse)));
1037
1038 if (RT_FAILURE(rcReq))
1039 Log2Func(("rcReq: %Rrc\n", rcReq));
1040
1041 if (LogIs3Enabled())
1042 {
1043 LogFunc(("cbDataIn = %u, cbDataOut = %u (cbIn = %u, cbOut = %u)\n",
1044 pReq->cbDataIn, pReq->cbDataOut, pReq->pVirtqBuf->cbPhysReturn, pReq->pVirtqBuf->cbPhysSend));
1045 LogFunc(("xfer = %lu, residual = %u\n", cbXfer, cbResidual));
1046 LogFunc(("xfer direction: %s, sense written = %d, sense size = %d\n",
1047 virtioGetTxDirText(pReq->enmTxDir), respHdr.cbSenseLen, pThis->virtioScsiConfig.uSenseSize));
1048 }
1049
1050 if (respHdr.cbSenseLen && LogIs2Enabled())
1051 {
1052 LogFunc(("Sense: %s\n", SCSISenseText(pReq->pbSense[2])));
1053 LogFunc(("Sense Ext3: %s\n", SCSISenseExtText(pReq->pbSense[12], pReq->pbSense[13])));
1054 }
1055
1056 if ( (VIRTIO_IS_IN_DIRECTION(pReq->enmTxDir) && cbXfer32 > pReq->cbDataIn)
1057 || (VIRTIO_IS_OUT_DIRECTION(pReq->enmTxDir) && cbXfer32 > pReq->cbDataOut))
1058 {
1059 Log2Func((" * * * * Data overrun, returning sense\n"));
1060 uint8_t abSense[] = { RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED,
1061 0, SCSI_SENSE_ILLEGAL_REQUEST, 0, 0, 0, 0, 10, 0, 0, 0 };
1062 respHdr.cbSenseLen = sizeof(abSense);
1063 respHdr.uStatus = SCSI_STATUS_CHECK_CONDITION;
1064 respHdr.uResponse = VIRTIOSCSI_S_OVERRUN;
1065 respHdr.uResidual = pReq->cbDataIn & UINT32_MAX;
1066
1067 virtioScsiR3ReqErr(pDevIns, pThis, pReq->uVirtqNbr, pReq->pVirtqBuf, &respHdr, abSense,
1068 RT_MIN(pThis->virtioScsiConfig.uSenseSize, VIRTIOSCSI_SENSE_SIZE_MAX));
1069 }
1070 else
1071 {
1072 Assert(pReq->pbSense != NULL);
1073
1074 /* req datain bytes already in guest phys mem. via virtioScsiIoReqCopyFromBuf() */
1075 RTSGSEG aReqSegs[2];
1076
1077 aReqSegs[0].pvSeg = &respHdr;
1078 aReqSegs[0].cbSeg = sizeof(respHdr);
1079
1080 aReqSegs[1].pvSeg = pReq->pbSense;
1081 aReqSegs[1].cbSeg = pReq->cbSenseAlloc; /* VirtIO 1.0 spec 5.6.4/5.6.6.1 */
1082
1083 RTSGBUF ReqSgBuf;
1084 RTSgBufInit(&ReqSgBuf, aReqSegs, RT_ELEMENTS(aReqSegs));
1085
1086 size_t cbReqSgBuf = RTSgBufCalcTotalLength(&ReqSgBuf);
1087 /** @todo r=bird: Returning here looks a little bogus... */
1088 AssertMsgReturn(cbReqSgBuf <= pReq->pVirtqBuf->cbPhysReturn,
1089 ("Guest expected less req data (space needed: %zu, avail: %u)\n",
1090 cbReqSgBuf, pReq->pVirtqBuf->cbPhysReturn),
1091 VERR_BUFFER_OVERFLOW);
1092
1093 virtioScsiR3VirtqUsedBufPutAndSync(pDevIns, &pThis->Virtio, pReq->uVirtqNbr, &ReqSgBuf, pReq->pVirtqBuf);
1094 Log2(("-----------------------------------------------------------------------------------------\n"));
1095 }
1096
1097 virtioScsiR3FreeReq(pTarget, pReq);
1098 virtioScsiR3Release(pDevIns, pThis, pThisCC);
1099 return rc;
1100}
1101
1102/**
1103 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyFromBuf}
1104
1105 * Copy virtual memory from VSCSI layer to guest physical memory
1106 */
1107static DECLCALLBACK(int) virtioScsiR3IoReqCopyFromBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1108 void *pvIoReqAlloc, uint32_t offDst, PRTSGBUF pSgBuf, size_t cbCopy)
1109{
1110 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaExPort);
1111 PPDMDEVINS pDevIns = pTarget->pDevIns;
1112 PVIRTIOSCSIREQ pReq = (PVIRTIOSCSIREQ)pvIoReqAlloc;
1113 RT_NOREF(hIoReq, cbCopy);
1114
1115 if (!pReq->cbDataIn)
1116 return VINF_SUCCESS;
1117
1118 AssertReturn(pReq->pVirtqBuf, VERR_INVALID_PARAMETER);
1119
1120 PVIRTIOSGBUF pSgPhysReturn = pReq->pVirtqBuf->pSgPhysReturn;
1121 virtioCoreGCPhysChainAdvance(pSgPhysReturn, offDst);
1122
1123 size_t cbCopied = 0;
1124 size_t cbRemain = pReq->cbDataIn;
1125
1126 /* Skip past the REQ_RESP_HDR_T and sense code if we're at the start of the buffer. */
1127 if (!pSgPhysReturn->idxSeg && pSgPhysReturn->cbSegLeft == pSgPhysReturn->paSegs[0].cbSeg)
1128 virtioCoreGCPhysChainAdvance(pSgPhysReturn, pReq->uDataInOff);
1129
1130 while (cbRemain)
1131 {
1132 cbCopied = RT_MIN(pSgBuf->cbSegLeft, pSgPhysReturn->cbSegLeft);
1133 Assert(cbCopied > 0);
1134 PDMDevHlpPCIPhysWriteUser(pDevIns, pSgPhysReturn->GCPhysCur, pSgBuf->pvSegCur, cbCopied);
1135 RTSgBufAdvance(pSgBuf, cbCopied);
1136 virtioCoreGCPhysChainAdvance(pSgPhysReturn, cbCopied);
1137 cbRemain -= cbCopied;
1138 }
1139 RT_UNTRUSTED_NONVOLATILE_COPY_FENCE(); /* needed? */
1140
1141 Log3Func((".... Copied %lu bytes from %lu byte guest buffer, residual=%lu\n",
1142 cbCopy, pReq->pVirtqBuf->cbPhysReturn, pReq->pVirtqBuf->cbPhysReturn - cbCopy));
1143
1144 return VINF_SUCCESS;
1145}
1146
1147/**
1148 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqCopyToBuf}
1149 *
1150 * Copy guest physical memory to VSCSI layer virtual memory
1151 */
1152static DECLCALLBACK(int) virtioScsiR3IoReqCopyToBuf(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
1153 void *pvIoReqAlloc, uint32_t offSrc, PRTSGBUF pSgBuf, size_t cbCopy)
1154{
1155 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaExPort);
1156 PPDMDEVINS pDevIns = pTarget->pDevIns;
1157 PVIRTIOSCSIREQ pReq = (PVIRTIOSCSIREQ)pvIoReqAlloc;
1158 RT_NOREF(hIoReq, cbCopy);
1159
1160 if (!pReq->cbDataOut)
1161 return VINF_SUCCESS;
1162
1163 PVIRTIOSGBUF pSgPhysSend = pReq->pVirtqBuf->pSgPhysSend;
1164 virtioCoreGCPhysChainAdvance(pSgPhysSend, offSrc);
1165
1166 size_t cbCopied = 0;
1167 size_t cbRemain = pReq->cbDataOut;
1168 while (cbRemain)
1169 {
1170 cbCopied = RT_MIN(pSgBuf->cbSegLeft, pSgPhysSend->cbSegLeft);
1171 Assert(cbCopied > 0);
1172 PDMDevHlpPCIPhysReadUser(pDevIns, pSgPhysSend->GCPhysCur, pSgBuf->pvSegCur, cbCopied);
1173 RTSgBufAdvance(pSgBuf, cbCopied);
1174 virtioCoreGCPhysChainAdvance(pSgPhysSend, cbCopied);
1175 cbRemain -= cbCopied;
1176 }
1177
1178 Log2Func((".... Copied %lu bytes to %lu byte guest buffer, residual=%lu\n",
1179 cbCopy, pReq->pVirtqBuf->cbPhysReturn, pReq->pVirtqBuf->cbPhysReturn - cbCopy));
1180
1181 return VINF_SUCCESS;
1182}
1183
1184/**
1185 * Handles request queues for/on a worker thread.
1186 *
1187 * @returns VBox status code (logged by caller).
1188 */
1189static int virtioScsiR3ReqSubmit(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, PVIRTIOSCSICC pThisCC,
1190 uint16_t uVirtqNbr, PVIRTQBUF pVirtqBuf)
1191{
1192 /*
1193 * Validate configuration values we use here before we start.
1194 */
1195 uint32_t const cbCdb = pThis->virtioScsiConfig.uCdbSize;
1196 uint32_t const cbSenseCfg = pThis->virtioScsiConfig.uSenseSize;
1197 /** @todo Report these as errors to the guest or does the caller do that? */
1198 ASSERT_GUEST_LOGREL_MSG_RETURN(cbCdb <= VIRTIOSCSI_CDB_SIZE_MAX, ("cbCdb=%#x\n", cbCdb), VERR_OUT_OF_RANGE);
1199 ASSERT_GUEST_LOGREL_MSG_RETURN(cbSenseCfg <= VIRTIOSCSI_SENSE_SIZE_MAX, ("cbSenseCfg=%#x\n", cbSenseCfg), VERR_OUT_OF_RANGE);
1200
1201 /*
1202 * Extract command header and CDB from guest physical memory
1203 * The max size is rather small here (19 + 255 = 274), so put
1204 * it on the stack.
1205 */
1206 size_t const cbReqHdr = sizeof(REQ_CMD_HDR_T) + cbCdb;
1207 AssertReturn(pVirtqBuf && pVirtqBuf->cbPhysSend >= cbReqHdr, VERR_INVALID_PARAMETER);
1208
1209 AssertCompile(VIRTIOSCSI_CDB_SIZE_MAX < 4096);
1210 union
1211 {
1212 RT_GCC_EXTENSION struct
1213 {
1214 REQ_CMD_HDR_T ReqHdr;
1215 uint8_t abCdb[VIRTIOSCSI_CDB_SIZE_MAX];
1216 } ;
1217 uint8_t ab[sizeof(REQ_CMD_HDR_T) + VIRTIOSCSI_CDB_SIZE_MAX];
1218 uint64_t au64Align[(sizeof(REQ_CMD_HDR_T) + VIRTIOSCSI_CDB_SIZE_MAX) / sizeof(uint64_t)];
1219 } VirtqReq;
1220 RT_ZERO(VirtqReq);
1221
1222 for (size_t offReq = 0; offReq < cbReqHdr; )
1223 {
1224 size_t cbSeg = cbReqHdr - offReq;
1225 RTGCPHYS GCPhys = virtioCoreGCPhysChainGetNextSeg(pVirtqBuf->pSgPhysSend, &cbSeg);
1226 PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhys, &VirtqReq.ab[offReq], cbSeg);
1227 offReq += cbSeg;
1228 }
1229
1230 uint8_t const uType = VirtqReq.ReqHdr.abVirtioLun[0];
1231 uint8_t const uTarget = VirtqReq.ReqHdr.abVirtioLun[1];
1232 uint32_t uScsiLun = RT_MAKE_U16(VirtqReq.ReqHdr.abVirtioLun[3], VirtqReq.ReqHdr.abVirtioLun[2]) & 0x3fff;
1233
1234 bool fBadLUNFormat = false;
1235 if (uType == 0xc1 && uTarget == 0x01)
1236 {
1237 LogRel(("* * * WARNING: REPORT LUNS LU ACCESSED. FEATURE NOT IMPLEMENTED SEE DevVirtioScsi.cpp * * * "));
1238 /* Force rejection. */ /** @todo figure out right way to handle. Note this is a very
1239 * vague and confusing part of the VirtIO spec (which deviates from the SCSI standard).
1240 * I have not been able to determine how to implement this properly. I've checked the
1241 * source code of Guest drivers, and so far none seem to use it. If this message is logged,
1242 * meaning a guest expects this feature, implementing it can be re-visited */
1243 uScsiLun = 0xff;
1244 }
1245 else
1246 if (uType != 1)
1247 fBadLUNFormat = true;
1248
1249 LogFunc(("[%s] (Target: %d LUN: %d) CDB: %.*Rhxs\n",
1250 SCSICmdText(VirtqReq.abCdb[0]), uTarget, uScsiLun,
1251 virtioScsiEstimateCdbLen(VirtqReq.abCdb[0], cbCdb), &VirtqReq.abCdb[0]));
1252
1253 Log3Func(("cmd id: %RX64, attr: %x, prio: %d, crn: %x\n",
1254 VirtqReq.ReqHdr.uId, VirtqReq.ReqHdr.uTaskAttr, VirtqReq.ReqHdr.uPrio, VirtqReq.ReqHdr.uCrn));
1255
1256 /*
1257 * Calculate request offsets and data sizes.
1258 */
1259 uint32_t const offDataOut = sizeof(REQ_CMD_HDR_T) + cbCdb;
1260 uint32_t const offDataIn = sizeof(REQ_RESP_HDR_T) + cbSenseCfg;
1261 size_t const cbDataOut = pVirtqBuf->cbPhysSend - offDataOut;
1262 /** @todo r=bird: Validate cbPhysReturn properly? I've just RT_MAX'ed it for now. */
1263 size_t const cbDataIn = RT_MAX(pVirtqBuf->cbPhysReturn, offDataIn) - offDataIn;
1264 Assert(offDataOut <= UINT16_MAX);
1265 Assert(offDataIn <= UINT16_MAX);
1266
1267 /*
1268 * Handle submission errors
1269 */
1270 if (RT_LIKELY(!fBadLUNFormat))
1271 { /* likely */ }
1272 else
1273 {
1274 Log2Func(("Error submitting request, bad LUN format\n"));
1275 return virtioScsiR3ReqErr4(pDevIns, pThis, uVirtqNbr, pVirtqBuf, cbDataIn + cbDataOut, 0 /*bStatus*/,
1276 VIRTIOSCSI_S_FAILURE, NULL /*pbSense*/, 0 /*cbSense*/, cbSenseCfg);
1277 }
1278
1279 PVIRTIOSCSITARGET const pTarget = &pThisCC->paTargetInstances[uTarget];
1280 if (RT_LIKELY( uTarget < pThis->cTargets
1281 && pTarget->fPresent
1282 && pTarget->pDrvMediaEx))
1283 { /* likely */ }
1284 else
1285 {
1286 Log2Func(("Error submitting request to bad target (%d) or bad LUN (%d)\n", uTarget, uScsiLun));
1287 uint8_t abSense[] = { RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED,
1288 0, SCSI_SENSE_ILLEGAL_REQUEST,
1289 0, 0, 0, 0, 10, SCSI_ASC_LOGICAL_UNIT_NOT_SUPPORTED, 0, 0 };
1290 return virtioScsiR3ReqErr4(pDevIns, pThis, uVirtqNbr, pVirtqBuf, cbDataIn + cbDataOut, SCSI_STATUS_CHECK_CONDITION,
1291 VIRTIOSCSI_S_BAD_TARGET, abSense, sizeof(abSense), cbSenseCfg);
1292 }
1293 if (RT_LIKELY(uScsiLun == 0))
1294 { /* likely */ }
1295 else
1296 {
1297 Log2Func(("Error submitting request to bad target (%d) or bad LUN (%d)\n", uTarget, uScsiLun));
1298 uint8_t abSense[] = { RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED,
1299 0, SCSI_SENSE_ILLEGAL_REQUEST,
1300 0, 0, 0, 0, 10, SCSI_ASC_LOGICAL_UNIT_NOT_SUPPORTED, 0, 0 };
1301 return virtioScsiR3ReqErr4(pDevIns, pThis, uVirtqNbr, pVirtqBuf, cbDataIn + cbDataOut, SCSI_STATUS_CHECK_CONDITION,
1302 VIRTIOSCSI_S_OK, abSense, sizeof(abSense), cbSenseCfg);
1303 }
1304 if (RT_LIKELY(!pThis->fResetting))
1305 { /* likely */ }
1306 else
1307 {
1308 Log2Func(("Aborting req submission because reset is in progress\n"));
1309 return virtioScsiR3ReqErr4(pDevIns, pThis, uVirtqNbr, pVirtqBuf, cbDataIn + cbDataOut, SCSI_STATUS_OK,
1310 VIRTIOSCSI_S_RESET, NULL /*pbSense*/, 0 /*cbSense*/, cbSenseCfg);
1311 }
1312
1313#if 0
1314 if (RT_LIKELY(!cbDataIn || !cbDataOut || pThis->fHasInOutBufs)) /* VirtIO 1.0, 5.6.6.1.1 */
1315 { /* likely */ }
1316 else
1317 {
1318 Log2Func(("Error submitting request, got datain & dataout bufs w/o INOUT feature negotated\n"));
1319 uint8_t abSense[] = { RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED,
1320 0, SCSI_SENSE_ILLEGAL_REQUEST, 0, 0, 0, 0, 10, 0, 0, 0 };
1321 return virtioScsiR3ReqErr4(pDevIns, pThis, uVirtqNbr, pVirtqBuf, cbDataIn + cbDataOut, SCSI_STATUS_CHECK_CONDITION,
1322 VIRTIOSCSI_S_FAILURE, abSense, sizeof(abSense), cbSenseCfg);
1323 }
1324#endif
1325 /*
1326 * Have underlying driver allocate a req of size set during initialization of this device.
1327 */
1328 virtioScsiR3Retain(pThis);
1329
1330 PDMMEDIAEXIOREQ hIoReq = NULL;
1331 PVIRTIOSCSIREQ pReq = NULL;
1332 PPDMIMEDIAEX pIMediaEx = pTarget->pDrvMediaEx;
1333
1334 int rc = pIMediaEx->pfnIoReqAlloc(pIMediaEx, &hIoReq, (void **)&pReq, 0 /* uIoReqId */,
1335 PDMIMEDIAEX_F_SUSPEND_ON_RECOVERABLE_ERR);
1336
1337 if (RT_FAILURE(rc))
1338 {
1339 virtioScsiR3Release(pDevIns, pThis, pThisCC);
1340 return rc;
1341 }
1342
1343 pReq->hIoReq = hIoReq;
1344 pReq->pTarget = pTarget;
1345 pReq->uVirtqNbr = uVirtqNbr;
1346 pReq->cbDataIn = cbDataIn;
1347 pReq->cbDataOut = cbDataOut;
1348 pReq->pVirtqBuf = pVirtqBuf;
1349 virtioCoreR3VirtqBufRetain(pVirtqBuf); /* (For pReq->pVirtqBuf. Released by virtioScsiR3FreeReq.) */
1350 pReq->uDataInOff = offDataIn;
1351 pReq->uDataOutOff = offDataOut;
1352
1353 pReq->cbSenseAlloc = cbSenseCfg;
1354 pReq->pbSense = (uint8_t *)RTMemAllocZ(pReq->cbSenseAlloc);
1355 AssertMsgReturnStmt(pReq->pbSense, ("Out of memory allocating sense buffer"),
1356 virtioScsiR3FreeReq(pTarget, pReq);, VERR_NO_MEMORY);
1357
1358 /* Note: DrvSCSI allocates one virtual memory buffer for input and output phases of the request */
1359 rc = pIMediaEx->pfnIoReqSendScsiCmd(pIMediaEx, pReq->hIoReq, uScsiLun,
1360 &VirtqReq.abCdb[0], cbCdb,
1361 PDMMEDIAEXIOREQSCSITXDIR_UNKNOWN, &pReq->enmTxDir,
1362 RT_MAX(cbDataIn, cbDataOut),
1363 pReq->pbSense, pReq->cbSenseAlloc, &pReq->cbSenseLen,
1364 &pReq->uStatus, RT_MS_30SEC);
1365
1366 if (rc != VINF_PDM_MEDIAEX_IOREQ_IN_PROGRESS)
1367 {
1368 /*
1369 * Getting here means the request failed in early in the submission to the lower level driver,
1370 * and there will be no callback to the finished/completion function for this request
1371 */
1372 Assert(RT_FAILURE_NP(rc));
1373 Log2Func(("Request-submission error from lower-level driver\n"));
1374 uint8_t uASC, uASCQ = 0;
1375 switch (rc)
1376 {
1377 case VERR_NO_MEMORY:
1378 uASC = SCSI_ASC_SYSTEM_RESOURCE_FAILURE;
1379 break;
1380 default:
1381 uASC = SCSI_ASC_INTERNAL_TARGET_FAILURE;
1382 break;
1383 }
1384 uint8_t abSense[] = { RT_BIT(7) | SCSI_SENSE_RESPONSE_CODE_CURR_FIXED,
1385 0, SCSI_SENSE_VENDOR_SPECIFIC,
1386 0, 0, 0, 0, 10, uASC, uASCQ, 0 };
1387 REQ_RESP_HDR_T respHdr = { 0 };
1388 respHdr.cbSenseLen = sizeof(abSense);
1389 respHdr.uStatus = SCSI_STATUS_CHECK_CONDITION;
1390 respHdr.uResponse = VIRTIOSCSI_S_FAILURE;
1391 respHdr.uResidual = (cbDataIn + cbDataOut) & UINT32_MAX;
1392 virtioScsiR3ReqErr(pDevIns, pThis, uVirtqNbr, pVirtqBuf, &respHdr, abSense, cbSenseCfg);
1393 virtioScsiR3FreeReq(pTarget, pReq);
1394 virtioScsiR3Release(pDevIns, pThis, pThisCC);
1395 }
1396 return VINF_SUCCESS;
1397}
1398
1399/**
1400 * Handles control transfers for/on a worker thread.
1401 *
1402 * @returns VBox status code (ignored by the caller).
1403 * @param pDevIns The device instance.
1404 * @param pThis VirtIO SCSI shared instance data.
1405 * @param pThisCC VirtIO SCSI ring-3 instance data.
1406 * @param uVirtqNbr CONTROLQ_IDX
1407 * @param pVirtqBuf Descriptor chain to process.
1408 */
1409static int virtioScsiR3Ctrl(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, PVIRTIOSCSICC pThisCC,
1410 uint16_t uVirtqNbr, PVIRTQBUF pVirtqBuf)
1411{
1412 AssertReturn(pVirtqBuf->cbPhysSend >= RT_MIN(sizeof(VIRTIOSCSI_CTRL_AN_T),
1413 sizeof(VIRTIOSCSI_CTRL_TMF_T)), 0);
1414
1415 /*
1416 * Allocate buffer and read in the control command
1417 */
1418 VIRTIO_SCSI_CTRL_UNION_T ScsiCtrlUnion;
1419 RT_ZERO(ScsiCtrlUnion);
1420
1421 size_t const cb = RT_MIN(pVirtqBuf->cbPhysSend, sizeof(VIRTIO_SCSI_CTRL_UNION_T));
1422 for (size_t uOffset = 0; uOffset < cb; )
1423 {
1424 size_t cbSeg = cb - uOffset;
1425 RTGCPHYS GCPhys = virtioCoreGCPhysChainGetNextSeg(pVirtqBuf->pSgPhysSend, &cbSeg);
1426 PDMDevHlpPCIPhysReadMeta(pDevIns, GCPhys, &ScsiCtrlUnion.ab[uOffset], cbSeg);
1427 uOffset += cbSeg;
1428 }
1429
1430 AssertReturn( (ScsiCtrlUnion.Type.uType == VIRTIOSCSI_T_TMF
1431 && pVirtqBuf->cbPhysSend >= sizeof(VIRTIOSCSI_CTRL_TMF_T))
1432 || ( ( ScsiCtrlUnion.Type.uType == VIRTIOSCSI_T_AN_QUERY
1433 || ScsiCtrlUnion.Type.uType == VIRTIOSCSI_T_AN_SUBSCRIBE)
1434 && pVirtqBuf->cbPhysSend >= sizeof(VIRTIOSCSI_CTRL_AN_T)),
1435 0 /** @todo r=bird: what kind of status is '0' here? */);
1436
1437 union
1438 {
1439 uint32_t fSupportedEvents;
1440 } uData;
1441 uint8_t bResponse = VIRTIOSCSI_S_OK;
1442 uint8_t cSegs;
1443 RTSGSEG aReqSegs[2];
1444 switch (ScsiCtrlUnion.Type.uType)
1445 {
1446 case VIRTIOSCSI_T_TMF: /* Task Management Functions */
1447 {
1448 uint8_t uTarget = ScsiCtrlUnion.Tmf.abScsiLun[1];
1449 uint32_t uScsiLun = RT_MAKE_U16(ScsiCtrlUnion.Tmf.abScsiLun[3], ScsiCtrlUnion.Tmf.abScsiLun[2]) & 0x3fff;
1450 Log2Func(("[%s] (Target: %d LUN: %d) Task Mgt Function: %s\n",
1451 VIRTQNAME(uVirtqNbr), uTarget, uScsiLun, virtioGetTMFTypeText(ScsiCtrlUnion.Tmf.uSubtype)));
1452
1453 if (uTarget >= pThis->cTargets || !pThisCC->paTargetInstances[uTarget].fPresent)
1454 bResponse = VIRTIOSCSI_S_BAD_TARGET;
1455 else
1456 if (uScsiLun != 0)
1457 bResponse = VIRTIOSCSI_S_INCORRECT_LUN;
1458 else
1459 switch (ScsiCtrlUnion.Tmf.uSubtype)
1460 {
1461 case VIRTIOSCSI_T_TMF_ABORT_TASK:
1462 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1463 break;
1464 case VIRTIOSCSI_T_TMF_ABORT_TASK_SET:
1465 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1466 break;
1467 case VIRTIOSCSI_T_TMF_CLEAR_ACA:
1468 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1469 break;
1470 case VIRTIOSCSI_T_TMF_CLEAR_TASK_SET:
1471 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1472 break;
1473 case VIRTIOSCSI_T_TMF_I_T_NEXUS_RESET:
1474 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1475 break;
1476 case VIRTIOSCSI_T_TMF_LOGICAL_UNIT_RESET:
1477 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED;
1478 break;
1479 case VIRTIOSCSI_T_TMF_QUERY_TASK:
1480 bResponse = VIRTIOSCSI_S_FUNCTION_REJECTED;
1481 break;
1482 case VIRTIOSCSI_T_TMF_QUERY_TASK_SET:
1483 bResponse = VIRTIOSCSI_S_FUNCTION_REJECTED;
1484 break;
1485 default:
1486 LogFunc(("Unknown TMF type\n"));
1487 bResponse = VIRTIOSCSI_S_FAILURE;
1488 }
1489 cSegs = 0; /* only bResponse */
1490 break;
1491 }
1492 case VIRTIOSCSI_T_AN_QUERY: /* Guest SCSI driver is querying supported async event notifications */
1493 {
1494 uint8_t uTarget = ScsiCtrlUnion.AsyncNotify.abScsiLun[1];
1495 uint32_t uScsiLun = RT_MAKE_U16(ScsiCtrlUnion.AsyncNotify.abScsiLun[3],
1496 ScsiCtrlUnion.AsyncNotify.abScsiLun[2]) & 0x3fff;
1497
1498 if (uTarget >= pThis->cTargets || !pThisCC->paTargetInstances[uTarget].fPresent)
1499 bResponse = VIRTIOSCSI_S_BAD_TARGET;
1500 else
1501 if (uScsiLun != 0)
1502 bResponse = VIRTIOSCSI_S_INCORRECT_LUN;
1503 else
1504 bResponse = VIRTIOSCSI_S_FUNCTION_COMPLETE;
1505
1506#ifdef LOG_ENABLED
1507 if (LogIs2Enabled())
1508 {
1509 char szTypeText[128];
1510 virtioGetControlAsyncMaskText(szTypeText, sizeof(szTypeText), ScsiCtrlUnion.AsyncNotify.fEventsRequested);
1511 Log2Func(("[%s] (Target: %d LUN: %d) Async. Notification Query: %s\n",
1512 VIRTQNAME(uVirtqNbr), uTarget, uScsiLun, szTypeText));
1513 }
1514#endif
1515 uData.fSupportedEvents = SUPPORTED_EVENTS;
1516 aReqSegs[0].pvSeg = &uData.fSupportedEvents;
1517 aReqSegs[0].cbSeg = sizeof(uData.fSupportedEvents);
1518 cSegs = 1;
1519 break;
1520 }
1521 case VIRTIOSCSI_T_AN_SUBSCRIBE: /* Guest SCSI driver is subscribing to async event notification(s) */
1522 {
1523 if (ScsiCtrlUnion.AsyncNotify.fEventsRequested & ~SUBSCRIBABLE_EVENTS)
1524 LogFunc(("Unsupported bits in event subscription event mask: %#x\n",
1525 ScsiCtrlUnion.AsyncNotify.fEventsRequested));
1526
1527 uint8_t uTarget = ScsiCtrlUnion.AsyncNotify.abScsiLun[1];
1528 uint32_t uScsiLun = RT_MAKE_U16(ScsiCtrlUnion.AsyncNotify.abScsiLun[3],
1529 ScsiCtrlUnion.AsyncNotify.abScsiLun[2]) & 0x3fff;
1530
1531#ifdef LOG_ENABLED
1532 if (LogIs2Enabled())
1533 {
1534 char szTypeText[128];
1535 virtioGetControlAsyncMaskText(szTypeText, sizeof(szTypeText), ScsiCtrlUnion.AsyncNotify.fEventsRequested);
1536 Log2Func(("[%s] (Target: %d LUN: %d) Async. Notification Subscribe: %s\n",
1537 VIRTQNAME(uVirtqNbr), uTarget, uScsiLun, szTypeText));
1538 }
1539#endif
1540 if (uTarget >= pThis->cTargets || !pThisCC->paTargetInstances[uTarget].fPresent)
1541 bResponse = VIRTIOSCSI_S_BAD_TARGET;
1542 else
1543 if (uScsiLun != 0)
1544 bResponse = VIRTIOSCSI_S_INCORRECT_LUN;
1545 else
1546 {
1547 bResponse = VIRTIOSCSI_S_FUNCTION_SUCCEEDED; /* or VIRTIOSCSI_S_FUNCTION_COMPLETE? */
1548 pThis->fAsyncEvtsEnabled = SUPPORTED_EVENTS & ScsiCtrlUnion.AsyncNotify.fEventsRequested;
1549 }
1550
1551 aReqSegs[0].pvSeg = &pThis->fAsyncEvtsEnabled;
1552 aReqSegs[0].cbSeg = sizeof(pThis->fAsyncEvtsEnabled);
1553 cSegs = 1;
1554 break;
1555 }
1556 default:
1557 {
1558 LogFunc(("Unknown control type extracted from %s: %u\n", VIRTQNAME(uVirtqNbr), ScsiCtrlUnion.Type.uType));
1559
1560 bResponse = VIRTIOSCSI_S_FAILURE;
1561 cSegs = 0; /* only bResponse */
1562 break;
1563 }
1564 }
1565
1566 /* Add the response code: */
1567 aReqSegs[cSegs].pvSeg = &bResponse;
1568 aReqSegs[cSegs].cbSeg = sizeof(bResponse);
1569 cSegs++;
1570 Assert(cSegs <= RT_ELEMENTS(aReqSegs));
1571
1572 LogFunc(("Response code: %s\n", virtioGetReqRespText(bResponse)));
1573
1574 RTSGBUF ReqSgBuf;
1575 RTSgBufInit(&ReqSgBuf, aReqSegs, cSegs);
1576
1577 virtioScsiR3VirtqUsedBufPutAndSync(pDevIns, &pThis->Virtio, uVirtqNbr, &ReqSgBuf, pVirtqBuf);
1578 return VINF_SUCCESS;
1579}
1580
1581/**
1582 * @callback_method_impl{FNPDMTHREADWAKEUPDEV}
1583 */
1584static DECLCALLBACK(int) virtioScsiR3WorkerWakeUp(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1585{
1586 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
1587 return PDMDevHlpSUPSemEventSignal(pDevIns, pThis->aWorkers[(uintptr_t)pThread->pvUser].hEvtProcess);
1588}
1589
1590/**
1591 * @callback_method_impl{FNPDMTHREADDEV}
1592 */
1593static DECLCALLBACK(int) virtioScsiR3WorkerThread(PPDMDEVINS pDevIns, PPDMTHREAD pThread)
1594{
1595 uint16_t const uVirtqNbr = (uint16_t)(uintptr_t)pThread->pvUser;
1596 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
1597 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
1598 PVIRTIOSCSIWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
1599 PVIRTIOSCSIWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uVirtqNbr];
1600
1601 if (pThread->enmState == PDMTHREADSTATE_INITIALIZING)
1602 return VINF_SUCCESS;
1603
1604 Log6Func(("[Re]starting %s worker\n", VIRTQNAME(uVirtqNbr)));
1605 while (pThread->enmState == PDMTHREADSTATE_RUNNING)
1606 {
1607 if ( !pWorkerR3->cRedoDescs
1608 && IS_VIRTQ_EMPTY(pDevIns, &pThis->Virtio, uVirtqNbr))
1609 {
1610 /* Atomic interlocks avoid missing alarm while going to sleep & notifier waking the awoken */
1611 ASMAtomicWriteBool(&pWorker->fSleeping, true);
1612 bool fNotificationSent = ASMAtomicXchgBool(&pWorker->fNotified, false);
1613 if (!fNotificationSent)
1614 {
1615 Log6Func(("%s worker sleeping...\n", VIRTQNAME(uVirtqNbr)));
1616 Assert(ASMAtomicReadBool(&pWorker->fSleeping));
1617 int rc = PDMDevHlpSUPSemEventWaitNoResume(pDevIns, pWorker->hEvtProcess, RT_INDEFINITE_WAIT);
1618 AssertLogRelMsgReturn(RT_SUCCESS(rc) || rc == VERR_INTERRUPTED, ("%Rrc\n", rc), rc);
1619 if (RT_UNLIKELY(pThread->enmState != PDMTHREADSTATE_RUNNING))
1620 {
1621 Log6Func(("%s worker thread not running, exiting\n", VIRTQNAME(uVirtqNbr)));
1622 return VINF_SUCCESS;
1623 }
1624 if (rc == VERR_INTERRUPTED)
1625 {
1626 Log6Func(("%s worker interrupted ... continuing\n", VIRTQNAME(uVirtqNbr)));
1627 continue;
1628 }
1629 Log6Func(("%s worker woken\n", VIRTQNAME(uVirtqNbr)));
1630 ASMAtomicWriteBool(&pWorker->fNotified, false);
1631 }
1632 ASMAtomicWriteBool(&pWorker->fSleeping, false);
1633 }
1634 if (!virtioCoreIsVirtqEnabled(&pThis->Virtio, uVirtqNbr))
1635 {
1636 LogFunc(("%s queue not enabled, worker aborting...\n", VIRTQNAME(uVirtqNbr)));
1637 break;
1638 }
1639
1640 if (!pThis->afVirtqAttached[uVirtqNbr])
1641 {
1642 LogFunc(("%s queue not attached, worker aborting...\n", VIRTQNAME(uVirtqNbr)));
1643 break;
1644 }
1645 if (!pThisCC->fQuiescing)
1646 {
1647 /* Process any reqs that were suspended saved to the redo queue in save exec. */
1648 for (int i = 0; i < pWorkerR3->cRedoDescs; i++)
1649 {
1650 PVIRTQBUF pVirtqBuf = virtioCoreR3VirtqBufAlloc();
1651 if (!pVirtqBuf)
1652 {
1653 LogRel(("Failed to allocate memory for VIRTQBUF\n"));
1654 break; /* No point in trying to allocate memory for other descriptor chains */
1655 }
1656 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, uVirtqNbr,
1657 pWorkerR3->auRedoDescs[i], pVirtqBuf);
1658 if (RT_FAILURE(rc))
1659 LogRel(("Error fetching desc chain to redo, %Rrc", rc));
1660
1661 rc = virtioScsiR3ReqSubmit(pDevIns, pThis, pThisCC, uVirtqNbr, pVirtqBuf);
1662 if (RT_FAILURE(rc))
1663 LogRel(("Error submitting req packet, resetting %Rrc", rc));
1664
1665 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
1666 }
1667 pWorkerR3->cRedoDescs = 0;
1668
1669 Log6Func(("fetching next descriptor chain from %s\n", VIRTQNAME(uVirtqNbr)));
1670 PVIRTQBUF pVirtqBuf = virtioCoreR3VirtqBufAlloc();
1671 if (!pVirtqBuf)
1672 LogRel(("Failed to allocate memory for VIRTQBUF\n"));
1673 else
1674 {
1675 int rc = virtioCoreR3VirtqAvailBufGet(pDevIns, &pThis->Virtio, uVirtqNbr, pVirtqBuf, true);
1676 if (rc == VERR_NOT_AVAILABLE)
1677 {
1678 Log6Func(("Nothing found in %s\n", VIRTQNAME(uVirtqNbr)));
1679 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
1680 continue;
1681 }
1682
1683 AssertRC(rc);
1684 if (uVirtqNbr == CONTROLQ_IDX)
1685 virtioScsiR3Ctrl(pDevIns, pThis, pThisCC, uVirtqNbr, pVirtqBuf);
1686 else /* request queue index */
1687 {
1688 rc = virtioScsiR3ReqSubmit(pDevIns, pThis, pThisCC, uVirtqNbr, pVirtqBuf);
1689 if (RT_FAILURE(rc))
1690 LogRel(("Error submitting req packet, resetting %Rrc", rc));
1691 }
1692
1693 virtioCoreR3VirtqBufRelease(&pThis->Virtio, pVirtqBuf);
1694 }
1695 }
1696 }
1697 return VINF_SUCCESS;
1698}
1699
1700
1701/*********************************************************************************************************************************
1702* Sending evnets
1703*********************************************************************************************************************************/
1704
1705/*
1706 * @todo Figure out how to implement this with R0 changes. Not used by current linux driver
1707 */
1708
1709#if 0
1710DECLINLINE(void) virtioScsiR3ReportEventsMissed(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
1711{
1712 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_NO_EVENT | VIRTIOSCSI_T_EVENTS_MISSED, 0);
1713}
1714#endif
1715
1716#if 0
1717/* SUBSCRIBABLE EVENT - not sure when to call this or how to detect when media is added or removed
1718 * via the VBox GUI */
1719DECLINLINE(void) virtioScsiR3ReportMediaChange(PPDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
1720{
1721 if (pThis->fAsyncEvtsEnabled & VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE)
1722 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_ASYNC_NOTIFY, VIRTIOSCSI_EVT_ASYNC_MEDIA_CHANGE);
1723}
1724
1725/* ESSENTIAL (NON-SUBSCRIBABLE) EVENT TYPES (most guest virtio-scsi drivers ignore?) */
1726
1727DECLINLINE(void) virtioScsiR3ReportTransportReset(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
1728{
1729 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_HARD);
1730}
1731
1732DECLINLINE(void) virtioScsiR3ReportParamChange(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget,
1733 uint32_t uSenseCode, uint32_t uSenseQualifier)
1734{
1735 uint32_t uReason = uSenseQualifier << 8 | uSenseCode;
1736 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_PARAM_CHANGE, uReason);
1737
1738}
1739
1740DECLINLINE(void) virtioScsiR3ReportTargetRemoved(PDMDEVINS pDevIns, PVIRTIOSCSI pThis, uint16_t uTarget)
1741{
1742 if (pThis->fHasHotplug)
1743 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_REMOVED);
1744}
1745
1746DECLINLINE(void) virtioScsiR3ReportTargetAdded(PDMDEVINS pDevInsPVIRTIOSCSI pThis, uint16_t uTarget)
1747{
1748 if (pThis->fHasHotplug)
1749 virtioScsiR3SendEvent(pDevIns, pThis, uTarget, VIRTIOSCSI_T_TRANSPORT_RESET, VIRTIOSCSI_EVT_RESET_RESCAN);
1750}
1751
1752#endif
1753
1754/**
1755 * @callback_method_impl{VIRTIOCORER3,pfnStatusChanged}
1756 */
1757static DECLCALLBACK(void) virtioScsiR3StatusChanged(PVIRTIOCORE pVirtio, PVIRTIOCORECC pVirtioCC, uint32_t fVirtioReady)
1758{
1759 PVIRTIOSCSI pThis = RT_FROM_MEMBER(pVirtio, VIRTIOSCSI, Virtio);
1760 PVIRTIOSCSICC pThisCC = RT_FROM_MEMBER(pVirtioCC, VIRTIOSCSICC, Virtio);
1761
1762 pThis->fVirtioReady = fVirtioReady;
1763
1764 if (fVirtioReady)
1765 {
1766 LogFunc(("VirtIO ready\n-----------------------------------------------------------------------------------------\n"));
1767 uint64_t fFeatures = virtioCoreGetNegotiatedFeatures(&pThis->Virtio);
1768 pThis->fHasT10pi = fFeatures & VIRTIO_SCSI_F_T10_PI;
1769 pThis->fHasHotplug = fFeatures & VIRTIO_SCSI_F_HOTPLUG;
1770 pThis->fHasInOutBufs = fFeatures & VIRTIO_SCSI_F_INOUT;
1771 pThis->fHasLunChange = fFeatures & VIRTIO_SCSI_F_CHANGE;
1772 pThis->fResetting = false;
1773 pThisCC->fQuiescing = false;
1774
1775 for (unsigned i = 0; i < VIRTIOSCSI_VIRTQ_CNT; i++)
1776 pThis->afVirtqAttached[i] = true;
1777 }
1778 else
1779 {
1780 LogFunc(("VirtIO is resetting\n"));
1781 for (unsigned i = 0; i < VIRTIOSCSI_VIRTQ_CNT; i++)
1782 pThis->afVirtqAttached[i] = false;
1783
1784 /*
1785 * BIOS may change these values. When the OS comes up, and KVM driver accessed
1786 * through Windows, it assumes they are the default size. So as per the VirtIO 1.0 spec,
1787 * 5.6.4, these device configuration values must be set to default upon device reset.
1788 */
1789 pThis->virtioScsiConfig.uSenseSize = VIRTIOSCSI_SENSE_SIZE_DEFAULT;
1790 pThis->virtioScsiConfig.uCdbSize = VIRTIOSCSI_CDB_SIZE_DEFAULT;
1791 }
1792
1793
1794}
1795
1796
1797/*********************************************************************************************************************************
1798* LEDs *
1799*********************************************************************************************************************************/
1800
1801/**
1802 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed, Target level.}
1803 */
1804static DECLCALLBACK(int) virtioScsiR3TargetQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
1805{
1806 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, ILed);
1807 if (iLUN == 0)
1808 {
1809 *ppLed = &pTarget->led;
1810 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
1811 return VINF_SUCCESS;
1812 }
1813 return VERR_PDM_LUN_NOT_FOUND;
1814}
1815/**
1816 * @interface_method_impl{PDMILEDPORTS,pfnQueryStatusLed, Device level.}
1817 */
1818static DECLCALLBACK(int) virtioScsiR3DeviceQueryStatusLed(PPDMILEDPORTS pInterface, unsigned iLUN, PPDMLED *ppLed)
1819{
1820 PVIRTIOSCSICC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIOSCSICC, ILeds);
1821 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pThisCC->pDevIns, PVIRTIOSCSI);
1822 if (iLUN < pThis->cTargets)
1823 {
1824 *ppLed = &pThisCC->paTargetInstances[iLUN].led;
1825 Assert((*ppLed)->u32Magic == PDMLED_MAGIC);
1826 return VINF_SUCCESS;
1827 }
1828 return VERR_PDM_LUN_NOT_FOUND;
1829}
1830
1831
1832/*********************************************************************************************************************************
1833* PDMIMEDIAPORT (target) *
1834*********************************************************************************************************************************/
1835
1836/**
1837 * @interface_method_impl{PDMIMEDIAPORT,pfnQueryDeviceLocation, Target level.}
1838 */
1839static DECLCALLBACK(int) virtioScsiR3QueryDeviceLocation(PPDMIMEDIAPORT pInterface, const char **ppcszController,
1840 uint32_t *piInstance, uint32_t *piLUN)
1841{
1842 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaPort);
1843 PPDMDEVINS pDevIns = pTarget->pDevIns;
1844
1845 AssertPtrReturn(ppcszController, VERR_INVALID_POINTER);
1846 AssertPtrReturn(piInstance, VERR_INVALID_POINTER);
1847 AssertPtrReturn(piLUN, VERR_INVALID_POINTER);
1848
1849 *ppcszController = pDevIns->pReg->szName;
1850 *piInstance = pDevIns->iInstance;
1851 *piLUN = pTarget->uTarget;
1852
1853 return VINF_SUCCESS;
1854}
1855
1856
1857/*********************************************************************************************************************************
1858* Virtio config. *
1859*********************************************************************************************************************************/
1860
1861/**
1862 * Worker for virtioScsiR3DevCapWrite and virtioScsiR3DevCapRead.
1863 */
1864static int virtioScsiR3CfgAccessed(PVIRTIOSCSI pThis, uint32_t uOffsetOfAccess, void *pv, uint32_t cb, bool fWrite)
1865{
1866 AssertReturn(pv && cb <= sizeof(uint32_t), fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00);
1867
1868 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uNumVirtqs, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1869 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uNumVirtqs, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1870 else
1871 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uSegMax, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1872 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uSegMax, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1873 else
1874 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMaxSectors, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1875 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMaxSectors, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1876 else
1877 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uCmdPerLun, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1878 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uCmdPerLun, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1879 else
1880 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uEventInfoSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1881 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uEventInfoSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1882 else
1883 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uSenseSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1884 VIRTIO_DEV_CONFIG_ACCESS( uSenseSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1885 else
1886 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uCdbSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1887 VIRTIO_DEV_CONFIG_ACCESS( uCdbSize, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1888 else
1889 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMaxChannel, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1890 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMaxChannel, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1891 else
1892 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMaxTarget, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1893 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMaxTarget, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1894 else
1895 if (VIRTIO_DEV_CONFIG_MATCH_MEMBER( uMaxLun, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess))
1896 VIRTIO_DEV_CONFIG_ACCESS_READONLY( uMaxLun, VIRTIOSCSI_CONFIG_T, uOffsetOfAccess, &pThis->virtioScsiConfig);
1897 else
1898 {
1899 LogFunc(("Bad access by guest to virtio_scsi_config: off=%u (%#x), cb=%u\n", uOffsetOfAccess, uOffsetOfAccess, cb));
1900 return fWrite ? VINF_SUCCESS : VINF_IOM_MMIO_UNUSED_00;
1901 }
1902 return VINF_SUCCESS;
1903}
1904
1905/**
1906 * @callback_method_impl{VIRTIOCORER3,pfnDevCapRead}
1907 */
1908static DECLCALLBACK(int) virtioScsiR3DevCapRead(PPDMDEVINS pDevIns, uint32_t uOffset, void *pv, uint32_t cb)
1909{
1910 return virtioScsiR3CfgAccessed(PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI), uOffset, pv, cb, false /*fRead*/);
1911}
1912
1913/**
1914 * @callback_method_impl{VIRTIOCORER3,pfnDevCapWrite}
1915 */
1916static DECLCALLBACK(int) virtioScsiR3DevCapWrite(PPDMDEVINS pDevIns, uint32_t uOffset, const void *pv, uint32_t cb)
1917{
1918 return virtioScsiR3CfgAccessed(PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI), uOffset, (void *)pv, cb, true /*fWrite*/);
1919}
1920
1921
1922/*********************************************************************************************************************************
1923* IBase for device and targets *
1924*********************************************************************************************************************************/
1925
1926/**
1927 * @interface_method_impl{PDMIBASE,pfnQueryInterface, Target level.}
1928 */
1929static DECLCALLBACK(void *) virtioScsiR3TargetQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1930{
1931 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IBase);
1932 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pTarget->IBase);
1933 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAPORT, &pTarget->IMediaPort);
1934 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIMEDIAEXPORT, &pTarget->IMediaExPort);
1935 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pTarget->ILed);
1936 return NULL;
1937}
1938
1939/**
1940 * @interface_method_impl{PDMIBASE,pfnQueryInterface, Device level.}
1941 */
1942static DECLCALLBACK(void *) virtioScsiR3DeviceQueryInterface(PPDMIBASE pInterface, const char *pszIID)
1943{
1944 PVIRTIOSCSICC pThisCC = RT_FROM_MEMBER(pInterface, VIRTIOSCSICC, IBase);
1945
1946 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pThisCC->IBase);
1947 PDMIBASE_RETURN_INTERFACE(pszIID, PDMILEDPORTS, &pThisCC->ILeds);
1948
1949 return NULL;
1950}
1951
1952
1953/*********************************************************************************************************************************
1954* Misc *
1955*********************************************************************************************************************************/
1956
1957/**
1958 * @callback_method_impl{FNDBGFHANDLERDEV, virtio-scsi debugger info callback.}
1959 */
1960static DECLCALLBACK(void) virtioScsiR3Info(PPDMDEVINS pDevIns, PCDBGFINFOHLP pHlp, const char *pszArgs)
1961{
1962 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
1963
1964 /* Parse arguments. */
1965 RT_NOREF(pszArgs); //bool fVerbose = pszArgs && strstr(pszArgs, "verbose") != NULL;
1966
1967 /* Show basic information. */
1968 pHlp->pfnPrintf(pHlp, "%s#%d: virtio-scsci ",
1969 pDevIns->pReg->szName,
1970 pDevIns->iInstance);
1971 pHlp->pfnPrintf(pHlp, "numTargets=%lu", pThis->cTargets);
1972}
1973
1974
1975/*********************************************************************************************************************************
1976* Saved state *
1977*********************************************************************************************************************************/
1978
1979/**
1980 * @callback_method_impl{FNSSMDEVLOADEXEC}
1981 */
1982static DECLCALLBACK(int) virtioScsiR3LoadExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM, uint32_t uVersion, uint32_t uPass)
1983{
1984 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
1985 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
1986 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
1987
1988 LogFunc(("LOAD EXEC!!\n"));
1989
1990 AssertReturn(uPass == SSM_PASS_FINAL, VERR_SSM_UNEXPECTED_PASS);
1991 AssertLogRelMsgReturn(uVersion == VIRTIOSCSI_SAVED_STATE_VERSION,
1992 ("uVersion=%u\n", uVersion), VERR_SSM_UNSUPPORTED_DATA_UNIT_VERSION);
1993
1994 virtioScsiSetVirtqNames(pThis);
1995 for (int uVirtqNbr = 0; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
1996 pHlp->pfnSSMGetBool(pSSM, &pThis->afVirtqAttached[uVirtqNbr]);
1997
1998 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uNumVirtqs);
1999 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uSegMax);
2000 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uMaxSectors);
2001 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uCmdPerLun);
2002 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uEventInfoSize);
2003 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uSenseSize);
2004 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uCdbSize);
2005 pHlp->pfnSSMGetU16(pSSM, &pThis->virtioScsiConfig.uMaxChannel);
2006 pHlp->pfnSSMGetU16(pSSM, &pThis->virtioScsiConfig.uMaxTarget);
2007 pHlp->pfnSSMGetU32(pSSM, &pThis->virtioScsiConfig.uMaxLun);
2008 pHlp->pfnSSMGetU32(pSSM, &pThis->fAsyncEvtsEnabled);
2009 pHlp->pfnSSMGetBool(pSSM, &pThis->fEventsMissed);
2010 pHlp->pfnSSMGetU32(pSSM, &pThis->fVirtioReady);
2011 pHlp->pfnSSMGetU32(pSSM, &pThis->fHasT10pi);
2012 pHlp->pfnSSMGetU32(pSSM, &pThis->fHasHotplug);
2013 pHlp->pfnSSMGetU32(pSSM, &pThis->fHasInOutBufs);
2014 pHlp->pfnSSMGetU32(pSSM, &pThis->fHasLunChange);
2015 pHlp->pfnSSMGetU32(pSSM, &pThis->fResetting);
2016
2017 uint32_t cTargets;
2018 int rc = pHlp->pfnSSMGetU32(pSSM, &cTargets);
2019 AssertRCReturn(rc, rc);
2020 AssertReturn(cTargets == pThis->cTargets,
2021 pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_LOAD_CONFIG_MISMATCH, RT_SRC_POS,
2022 N_("target count has changed: %u saved, %u configured now"),
2023 cTargets, pThis->cTargets));
2024
2025 for (uint16_t uTarget = 0; uTarget < pThis->cTargets; uTarget++)
2026 {
2027 uint16_t cReqsRedo;
2028 rc = pHlp->pfnSSMGetU16(pSSM, &cReqsRedo);
2029 AssertRCReturn(rc, rc);
2030 AssertReturn(cReqsRedo < VIRTQ_SIZE,
2031 pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2032 N_("Bad count of I/O transactions to re-do in saved state (%#x, max %#x - 1)"),
2033 cReqsRedo, VIRTQ_SIZE));
2034
2035 for (uint16_t uVirtqNbr = VIRTQ_REQ_BASE; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
2036 {
2037 PVIRTIOSCSIWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uVirtqNbr];
2038 pWorkerR3->cRedoDescs = 0;
2039 }
2040
2041 for (int i = 0; i < cReqsRedo; i++)
2042 {
2043 uint16_t uVirtqNbr;
2044 rc = pHlp->pfnSSMGetU16(pSSM, &uVirtqNbr);
2045 AssertRCReturn(rc, rc);
2046 AssertReturn(uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT,
2047 pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2048 N_("Bad queue index for re-do in saved state (%#x, max %#x)"),
2049 uVirtqNbr, VIRTIOSCSI_VIRTQ_CNT - 1));
2050
2051 uint16_t idxHead;
2052 rc = pHlp->pfnSSMGetU16(pSSM, &idxHead);
2053 AssertRCReturn(rc, rc);
2054 AssertReturn(idxHead < VIRTQ_SIZE,
2055 pHlp->pfnSSMSetLoadError(pSSM, VERR_SSM_DATA_UNIT_FORMAT_CHANGED, RT_SRC_POS,
2056 N_("Bad queue element index for re-do in saved state (%#x, max %#x)"),
2057 idxHead, VIRTQ_SIZE - 1));
2058
2059 PVIRTIOSCSIWORKERR3 pWorkerR3 = &pThisCC->aWorkers[uVirtqNbr];
2060 pWorkerR3->auRedoDescs[pWorkerR3->cRedoDescs++] = idxHead;
2061 pWorkerR3->cRedoDescs %= VIRTQ_SIZE;
2062 }
2063 }
2064
2065 /*
2066 * Call the virtio core to let it load its state.
2067 */
2068 rc = virtioCoreR3ModernDeviceLoadExec(&pThis->Virtio, pDevIns->pHlpR3, pSSM,
2069 uVersion, VIRTIOSCSI_SAVED_STATE_VERSION, pThis->virtioScsiConfig.uNumVirtqs);
2070
2071 /*
2072 * Nudge request queue workers
2073 */
2074 for (int uVirtqNbr = VIRTQ_REQ_BASE; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
2075 {
2076 if (pThis->afVirtqAttached[uVirtqNbr])
2077 {
2078 LogFunc(("Waking %s worker.\n", VIRTQNAME(uVirtqNbr)));
2079 int rc2 = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->aWorkers[uVirtqNbr].hEvtProcess);
2080 AssertRCReturn(rc, rc2);
2081 }
2082 }
2083
2084 return rc;
2085}
2086
2087/**
2088 * @callback_method_impl{FNSSMDEVSAVEEXEC}
2089 */
2090static DECLCALLBACK(int) virtioScsiR3SaveExec(PPDMDEVINS pDevIns, PSSMHANDLE pSSM)
2091{
2092 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2093 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2094 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
2095
2096 LogFunc(("SAVE EXEC!!\n"));
2097
2098 for (int uVirtqNbr = 0; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
2099 pHlp->pfnSSMPutBool(pSSM, pThis->afVirtqAttached[uVirtqNbr]);
2100
2101 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uNumVirtqs);
2102 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uSegMax);
2103 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uMaxSectors);
2104 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uCmdPerLun);
2105 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uEventInfoSize);
2106 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uSenseSize);
2107 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uCdbSize);
2108 pHlp->pfnSSMPutU16(pSSM, pThis->virtioScsiConfig.uMaxChannel);
2109 pHlp->pfnSSMPutU16(pSSM, pThis->virtioScsiConfig.uMaxTarget);
2110 pHlp->pfnSSMPutU32(pSSM, pThis->virtioScsiConfig.uMaxLun);
2111 pHlp->pfnSSMPutU32(pSSM, pThis->fAsyncEvtsEnabled);
2112 pHlp->pfnSSMPutBool(pSSM, pThis->fEventsMissed);
2113 pHlp->pfnSSMPutU32(pSSM, pThis->fVirtioReady);
2114 pHlp->pfnSSMPutU32(pSSM, pThis->fHasT10pi);
2115 pHlp->pfnSSMPutU32(pSSM, pThis->fHasHotplug);
2116 pHlp->pfnSSMPutU32(pSSM, pThis->fHasInOutBufs);
2117 pHlp->pfnSSMPutU32(pSSM, pThis->fHasLunChange);
2118 pHlp->pfnSSMPutU32(pSSM, pThis->fResetting);
2119
2120 AssertMsg(!pThis->cActiveReqs, ("There are still outstanding requests on this device\n"));
2121
2122 pHlp->pfnSSMPutU32(pSSM, pThis->cTargets);
2123
2124 for (uint16_t uTarget = 0; uTarget < pThis->cTargets; uTarget++)
2125 {
2126 PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
2127
2128 /* Query all suspended requests and store them in the request queue. */
2129 if (pTarget->pDrvMediaEx)
2130 {
2131 uint32_t cReqsRedo = pTarget->pDrvMediaEx->pfnIoReqGetSuspendedCount(pTarget->pDrvMediaEx);
2132
2133 pHlp->pfnSSMPutU16(pSSM, cReqsRedo);
2134
2135 if (cReqsRedo)
2136 {
2137 PDMMEDIAEXIOREQ hIoReq;
2138 PVIRTIOSCSIREQ pReq;
2139
2140 int rc = pTarget->pDrvMediaEx->pfnIoReqQuerySuspendedStart(pTarget->pDrvMediaEx, &hIoReq,
2141 (void **)&pReq);
2142 AssertRCBreak(rc);
2143
2144 while(--cReqsRedo)
2145 {
2146 pHlp->pfnSSMPutU16(pSSM, pReq->uVirtqNbr);
2147 pHlp->pfnSSMPutU16(pSSM, pReq->pVirtqBuf->uHeadIdx);
2148
2149 rc = pTarget->pDrvMediaEx->pfnIoReqQuerySuspendedNext(pTarget->pDrvMediaEx, hIoReq,
2150 &hIoReq, (void **)&pReq);
2151 AssertRCBreak(rc);
2152 }
2153 }
2154 }
2155 }
2156
2157 /*
2158 * Call the virtio core to let it save its state.
2159 */
2160 return virtioCoreR3SaveExec(&pThis->Virtio, pDevIns->pHlpR3, pSSM, VIRTIOSCSI_SAVED_STATE_VERSION, VIRTIOSCSI_VIRTQ_CNT);
2161}
2162
2163
2164/*********************************************************************************************************************************
2165* Device interface. *
2166*********************************************************************************************************************************/
2167
2168/**
2169 * @interface_method_impl{PDMDEVREGR3,pfnDetach}
2170 *
2171 * One harddisk at one port has been unplugged.
2172 * The VM is suspended at this point.
2173 */
2174static DECLCALLBACK(void) virtioScsiR3Detach(PPDMDEVINS pDevIns, unsigned uTarget, uint32_t fFlags)
2175{
2176 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2177 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2178 AssertReturnVoid(uTarget < pThis->cTargets);
2179 PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
2180
2181 LogFunc((""));
2182
2183 AssertMsg(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2184 ("virtio-scsi: Device does not support hotplugging\n"));
2185 RT_NOREF(fFlags);
2186
2187 /*
2188 * Zero all important members.
2189 */
2190 pTarget->fPresent = false;
2191 pTarget->pDrvBase = NULL;
2192 pTarget->pDrvMedia = NULL;
2193 pTarget->pDrvMediaEx = NULL;
2194}
2195
2196/**
2197 * @interface_method_impl{PDMDEVREGR3,pfnAttach}
2198 *
2199 * This is called when we change block driver.
2200 */
2201static DECLCALLBACK(int) virtioScsiR3Attach(PPDMDEVINS pDevIns, unsigned uTarget, uint32_t fFlags)
2202{
2203 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2204 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2205 AssertReturn(uTarget < pThis->cTargets, VERR_PDM_LUN_NOT_FOUND);
2206 PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
2207
2208 Assert(pTarget->pDevIns == pDevIns);
2209 AssertMsgReturn(fFlags & PDM_TACH_FLAGS_NOT_HOT_PLUG,
2210 ("virtio-scsi: Device does not support hotplugging\n"),
2211 VERR_INVALID_PARAMETER);
2212
2213 AssertRelease(!pTarget->pDrvBase);
2214 Assert(pTarget->uTarget == uTarget);
2215
2216 /*
2217 * Try attach the SCSI driver and get the interfaces, required as well as optional.
2218 */
2219 int rc = PDMDevHlpDriverAttach(pDevIns, pTarget->uTarget, &pDevIns->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
2220 if (RT_SUCCESS(rc))
2221 {
2222 pTarget->fPresent = true;
2223 pTarget->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIA);
2224 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMedia),
2225 ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", uTarget),
2226 VERR_PDM_MISSING_INTERFACE);
2227
2228 /* Get the extended media interface. */
2229 pTarget->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIAEX);
2230 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMediaEx),
2231 ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", uTarget),
2232 VERR_PDM_MISSING_INTERFACE);
2233
2234 rc = pTarget->pDrvMediaEx->pfnIoReqAllocSizeSet(pTarget->pDrvMediaEx, sizeof(VIRTIOSCSIREQ));
2235 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMediaEx),
2236 ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", uTarget),
2237 rc);
2238 }
2239 else
2240 AssertMsgFailed(("Failed to attach %s. rc=%Rrc\n", pTarget->pszTargetName, rc));
2241
2242 if (RT_FAILURE(rc))
2243 {
2244 pTarget->fPresent = false;
2245 pTarget->pDrvBase = NULL;
2246 pTarget->pDrvMedia = NULL;
2247 pTarget->pDrvMediaEx = NULL;
2248 pThisCC->pMediaNotify = NULL;
2249 }
2250 return rc;
2251}
2252
2253/**
2254 * @callback_method_impl{FNPDMDEVASYNCNOTIFY}
2255 */
2256static DECLCALLBACK(bool) virtioScsiR3DeviceQuiesced(PPDMDEVINS pDevIns)
2257{
2258 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2259 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2260
2261 if (ASMAtomicReadU32(&pThis->cActiveReqs))
2262 return false;
2263
2264 LogFunc(("Device I/O activity quiesced: %s\n",
2265 virtioCoreGetStateChangeText(pThisCC->enmQuiescingFor)));
2266
2267 virtioCoreR3VmStateChanged(&pThis->Virtio, pThisCC->enmQuiescingFor);
2268
2269 pThis->fResetting = false;
2270 pThisCC->fQuiescing = false;
2271
2272 return true;
2273}
2274
2275/**
2276 * Worker for virtioScsiR3Reset() and virtioScsiR3SuspendOrPowerOff().
2277 */
2278static void virtioScsiR3QuiesceDevice(PPDMDEVINS pDevIns, VIRTIOVMSTATECHANGED enmQuiscingFor)
2279{
2280 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2281 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2282
2283 /* Prevent worker threads from removing/processing elements from virtq's */
2284 pThisCC->fQuiescing = true;
2285 pThisCC->enmQuiescingFor = enmQuiscingFor;
2286
2287 PDMDevHlpSetAsyncNotification(pDevIns, virtioScsiR3DeviceQuiesced);
2288
2289 /* If already quiesced invoke async callback. */
2290 if (!ASMAtomicReadU32(&pThis->cActiveReqs))
2291 PDMDevHlpAsyncNotificationCompleted(pDevIns);
2292}
2293
2294/**
2295 * @interface_method_impl{PDMDEVREGR3,pfnReset}
2296 */
2297static DECLCALLBACK(void) virtioScsiR3Reset(PPDMDEVINS pDevIns)
2298{
2299 LogFunc(("\n"));
2300 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2301 pThis->fResetting = true;
2302 virtioScsiR3QuiesceDevice(pDevIns, kvirtIoVmStateChangedReset);
2303}
2304
2305/**
2306 * @interface_method_impl{PDMDEVREGR3,pfnPowerOff}
2307 */
2308static DECLCALLBACK(void) virtioScsiR3SuspendOrPowerOff(PPDMDEVINS pDevIns, VIRTIOVMSTATECHANGED enmType)
2309{
2310 LogFunc(("\n"));
2311
2312 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2313 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2314
2315 /* VM is halted, thus no new I/O being dumped into queues by the guest.
2316 * Workers have been flagged to stop pulling stuff already queued-up by the guest.
2317 * Now tell lower-level to to suspend reqs (for example, DrvVD suspends all reqs
2318 * on its wait queue, and we will get a callback as the state changes to
2319 * suspended (and later, resumed) for each).
2320 */
2321 for (uint32_t i = 0; i < pThis->cTargets; i++)
2322 {
2323 PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[i];
2324 if (pTarget->pDrvMediaEx)
2325 pTarget->pDrvMediaEx->pfnNotifySuspend(pTarget->pDrvMediaEx);
2326 }
2327
2328 virtioScsiR3QuiesceDevice(pDevIns, enmType);
2329}
2330
2331/**
2332 * @interface_method_impl{PDMDEVREGR3,pfnSuspend}
2333 */
2334static DECLCALLBACK(void) virtioScsiR3PowerOff(PPDMDEVINS pDevIns)
2335{
2336 LogFunc(("\n"));
2337 virtioScsiR3SuspendOrPowerOff(pDevIns, kvirtIoVmStateChangedPowerOff);
2338}
2339
2340/**
2341 * @interface_method_impl{PDMDEVREGR3,pfnSuspend}
2342 */
2343static DECLCALLBACK(void) virtioScsiR3Suspend(PPDMDEVINS pDevIns)
2344{
2345 LogFunc(("\n"));
2346 virtioScsiR3SuspendOrPowerOff(pDevIns, kvirtIoVmStateChangedSuspend);
2347}
2348
2349/**
2350 * @interface_method_impl{PDMDEVREGR3,pfnResume}
2351 */
2352static DECLCALLBACK(void) virtioScsiR3Resume(PPDMDEVINS pDevIns)
2353{
2354 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2355 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2356 LogFunc(("\n"));
2357
2358 pThisCC->fQuiescing = false;
2359
2360 /* Wake worker threads flagged to skip pulling queue entries during quiesce
2361 * to ensure they re-check their queues. Active request queues may already
2362 * be awake due to new reqs coming in.
2363 */
2364 for (uint16_t uVirtqNbr = 0; uVirtqNbr < VIRTIOSCSI_REQ_VIRTQ_CNT; uVirtqNbr++)
2365 {
2366 if ( virtioCoreIsVirtqEnabled(&pThis->Virtio, uVirtqNbr)
2367 && ASMAtomicReadBool(&pThis->aWorkers[uVirtqNbr].fSleeping))
2368 {
2369 Log6Func(("waking %s worker.\n", VIRTQNAME(uVirtqNbr)));
2370 int rc = PDMDevHlpSUPSemEventSignal(pDevIns, pThis->aWorkers[uVirtqNbr].hEvtProcess);
2371 AssertRC(rc);
2372 }
2373 }
2374 /* Ensure guest is working the queues too. */
2375 virtioCoreR3VmStateChanged(&pThis->Virtio, kvirtIoVmStateChangedResume);
2376}
2377
2378/**
2379 * @interface_method_impl{PDMIMEDIAEXPORT,pfnMediumEjected}
2380 */
2381static DECLCALLBACK(void) virtioScsiR3MediumEjected(PPDMIMEDIAEXPORT pInterface)
2382{
2383 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaExPort);
2384 PPDMDEVINS pDevIns = pTarget->pDevIns;
2385 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2386
2387#if 0 /* need more info about how to use this event. The VirtIO 1.0 specification
2388 * lists several SCSI related event types but presumes the reader knows
2389 * how to use them without providing references. */
2390 virtioScsiR3ReportMediaChange(pDevIns, pThis, pTarget->uTarget);
2391#endif
2392
2393 if (pThisCC->pMediaNotify)
2394 {
2395 int rc = PDMDevHlpVMReqCallNoWait(pDevIns, VMCPUID_ANY,
2396 (PFNRT)pThisCC->pMediaNotify->pfnEjected, 2,
2397 pThisCC->pMediaNotify, pTarget->uTarget);
2398 AssertRC(rc);
2399 }
2400}
2401
2402/**
2403 * @interface_method_impl{PDMIMEDIAEXPORT,pfnIoReqStateChanged}
2404 */
2405static DECLCALLBACK(void) virtioScsiR3IoReqStateChanged(PPDMIMEDIAEXPORT pInterface, PDMMEDIAEXIOREQ hIoReq,
2406 void *pvIoReqAlloc, PDMMEDIAEXIOREQSTATE enmState)
2407{
2408 PVIRTIOSCSITARGET pTarget = RT_FROM_MEMBER(pInterface, VIRTIOSCSITARGET, IMediaExPort);
2409 PPDMDEVINS pDevIns = pTarget->pDevIns;
2410 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2411 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2412 RT_NOREF(hIoReq, pvIoReqAlloc);
2413
2414 switch (enmState)
2415 {
2416 case PDMMEDIAEXIOREQSTATE_SUSPENDED:
2417 {
2418 /* Stop considering this request active */
2419 virtioScsiR3Release(pDevIns, pThis, pThisCC);
2420 break;
2421 }
2422 case PDMMEDIAEXIOREQSTATE_ACTIVE:
2423 virtioScsiR3Retain(pThis);
2424 break;
2425 default:
2426 AssertMsgFailed(("Invalid request state given %u\n", enmState));
2427 }
2428}
2429
2430/**
2431 * @interface_method_impl{PDMDEVREGR3,pfnDestruct}
2432 */
2433static DECLCALLBACK(int) virtioScsiR3Destruct(PPDMDEVINS pDevIns)
2434{
2435 PDMDEV_CHECK_VERSIONS_RETURN_QUIET(pDevIns);
2436 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2437 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2438
2439 RTMemFree(pThisCC->paTargetInstances);
2440 pThisCC->paTargetInstances = NULL;
2441 pThisCC->pMediaNotify = NULL;
2442
2443 for (unsigned uVirtqNbr = 0; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
2444 {
2445 PVIRTIOSCSIWORKER pWorker = &pThis->aWorkers[uVirtqNbr];
2446 if (pWorker->hEvtProcess != NIL_SUPSEMEVENT)
2447 {
2448 PDMDevHlpSUPSemEventClose(pDevIns, pWorker->hEvtProcess);
2449 pWorker->hEvtProcess = NIL_SUPSEMEVENT;
2450 }
2451
2452 if (pThisCC->aWorkers[uVirtqNbr].pThread)
2453 {
2454 /* Destroy the thread. */
2455 int rcThread;
2456 int rc = PDMDevHlpThreadDestroy(pDevIns, pThisCC->aWorkers[uVirtqNbr].pThread, &rcThread);
2457 if (RT_FAILURE(rc) || RT_FAILURE(rcThread))
2458 AssertMsgFailed(("%s Failed to destroythread rc=%Rrc rcThread=%Rrc\n",
2459 __FUNCTION__, rc, rcThread));
2460 pThisCC->aWorkers[uVirtqNbr].pThread = NULL;
2461 }
2462
2463 if (RTCritSectIsInitialized(&pThisCC->aWorkers[uVirtqNbr].CritSectVirtq))
2464 RTCritSectDelete(&pThisCC->aWorkers[uVirtqNbr].CritSectVirtq);
2465 }
2466
2467 virtioCoreR3Term(pDevIns, &pThis->Virtio, &pThisCC->Virtio);
2468 return VINF_SUCCESS;
2469}
2470
2471/**
2472 * @interface_method_impl{PDMDEVREGR3,pfnConstruct}
2473 */
2474static DECLCALLBACK(int) virtioScsiR3Construct(PPDMDEVINS pDevIns, int iInstance, PCFGMNODE pCfg)
2475{
2476 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2477 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2478 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2479 PCPDMDEVHLPR3 pHlp = pDevIns->pHlpR3;
2480
2481 /*
2482 * Quick initialization of the state data, making sure that the destructor always works.
2483 */
2484 pThisCC->pDevIns = pDevIns;
2485
2486 LogFunc(("PDM device instance: %d\n", iInstance));
2487 RTStrPrintf(pThis->szInstance, sizeof(pThis->szInstance), "VIRTIOSCSI%d", iInstance);
2488
2489 pThisCC->IBase.pfnQueryInterface = virtioScsiR3DeviceQueryInterface;
2490 pThisCC->ILeds.pfnQueryStatusLed = virtioScsiR3DeviceQueryStatusLed;
2491
2492 /*
2493 * Validate and read configuration.
2494 */
2495 PDMDEV_VALIDATE_CONFIG_RETURN(pDevIns, "NumTargets"
2496 "|Bootable"
2497 "|MmioBase"
2498 "|Irq", "");
2499
2500 int rc = pHlp->pfnCFGMQueryU32Def(pCfg, "NumTargets", &pThis->cTargets, 1);
2501 if (RT_FAILURE(rc))
2502 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio-scsi configuration error: failed to read NumTargets as integer"));
2503 if (pThis->cTargets < 1 || pThis->cTargets > VIRTIOSCSI_MAX_TARGETS)
2504 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2505 N_("virtio-scsi configuration error: NumTargets=%u is out of range (1..%u)"),
2506 pThis->cTargets, VIRTIOSCSI_MAX_TARGETS);
2507
2508 rc = pHlp->pfnCFGMQueryBoolDef(pCfg, "Bootable", &pThis->fBootable, true);
2509 if (RT_FAILURE(rc))
2510 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio-scsi configuration error: failed to read Bootable as boolean"));
2511
2512 LogRel(("%s: Targets=%u Bootable=%RTbool (unimplemented) R0Enabled=%RTbool RCEnabled=%RTbool\n",
2513 pThis->szInstance, pThis->cTargets, pThis->fBootable, pDevIns->fR0Enabled, pDevIns->fRCEnabled));
2514
2515
2516 /*
2517 * Do core virtio initialization.
2518 */
2519
2520 /* Configure virtio_scsi_config that transacts via VirtIO implementation's Dev. Specific Cap callbacks */
2521 pThis->virtioScsiConfig.uNumVirtqs = VIRTIOSCSI_REQ_VIRTQ_CNT;
2522 pThis->virtioScsiConfig.uSegMax = VIRTIOSCSI_MAX_SEG_COUNT;
2523 pThis->virtioScsiConfig.uMaxSectors = VIRTIOSCSI_MAX_SECTORS_HINT;
2524 pThis->virtioScsiConfig.uCmdPerLun = VIRTIOSCSI_MAX_COMMANDS_PER_LUN;
2525 pThis->virtioScsiConfig.uEventInfoSize = sizeof(VIRTIOSCSI_EVENT_T); /*VirtIO 1.0 Spec says at least this size! */
2526 pThis->virtioScsiConfig.uSenseSize = VIRTIOSCSI_SENSE_SIZE_DEFAULT;
2527 pThis->virtioScsiConfig.uCdbSize = VIRTIOSCSI_CDB_SIZE_DEFAULT;
2528 pThis->virtioScsiConfig.uMaxChannel = VIRTIOSCSI_MAX_CHANNEL_HINT;
2529 pThis->virtioScsiConfig.uMaxTarget = pThis->cTargets;
2530 pThis->virtioScsiConfig.uMaxLun = VIRTIOSCSI_MAX_LUN;
2531
2532 /* Initialize the generic Virtio core: */
2533 pThisCC->Virtio.pfnVirtqNotified = virtioScsiNotified;
2534 pThisCC->Virtio.pfnStatusChanged = virtioScsiR3StatusChanged;
2535 pThisCC->Virtio.pfnDevCapRead = virtioScsiR3DevCapRead;
2536 pThisCC->Virtio.pfnDevCapWrite = virtioScsiR3DevCapWrite;
2537
2538 VIRTIOPCIPARAMS VirtioPciParams;
2539 VirtioPciParams.uDeviceId = PCI_DEVICE_ID_VIRTIOSCSI_HOST;
2540 VirtioPciParams.uClassBase = PCI_CLASS_BASE_MASS_STORAGE;
2541 VirtioPciParams.uClassSub = PCI_CLASS_SUB_SCSI_STORAGE_CONTROLLER;
2542 VirtioPciParams.uClassProg = PCI_CLASS_PROG_UNSPECIFIED;
2543 VirtioPciParams.uSubsystemId = PCI_DEVICE_ID_VIRTIOSCSI_HOST; /* VirtIO 1.0 spec allows PCI Device ID here */
2544 VirtioPciParams.uInterruptLine = 0x00;
2545 VirtioPciParams.uInterruptPin = 0x01;
2546 VirtioPciParams.uDeviceType = VIRTIO_DEVICE_TYPE_SCSI_HOST;
2547
2548 rc = virtioCoreR3Init(pDevIns, &pThis->Virtio, &pThisCC->Virtio, &VirtioPciParams, pThis->szInstance,
2549 VIRTIOSCSI_HOST_SCSI_FEATURES_OFFERED, 0 /*fOfferLegacy*/,
2550 &pThis->virtioScsiConfig /*pvDevSpecificCap*/, sizeof(pThis->virtioScsiConfig));
2551 if (RT_FAILURE(rc))
2552 return PDMDEV_SET_ERROR(pDevIns, rc, N_("virtio-scsi: failed to initialize VirtIO"));
2553
2554 /*
2555 * Initialize queues.
2556 */
2557
2558 virtioScsiSetVirtqNames(pThis);
2559
2560 /* Attach the queues and create worker threads for them: */
2561 for (uint16_t uVirtqNbr = 0; uVirtqNbr < VIRTIOSCSI_VIRTQ_CNT; uVirtqNbr++)
2562 {
2563 rc = virtioCoreR3VirtqAttach(&pThis->Virtio, uVirtqNbr, VIRTQNAME(uVirtqNbr));
2564 if (RT_FAILURE(rc))
2565 continue;
2566 if (uVirtqNbr == CONTROLQ_IDX || IS_REQ_VIRTQ(uVirtqNbr))
2567 {
2568 rc = PDMDevHlpThreadCreate(pDevIns, &pThisCC->aWorkers[uVirtqNbr].pThread,
2569 (void *)(uintptr_t)uVirtqNbr, virtioScsiR3WorkerThread,
2570 virtioScsiR3WorkerWakeUp, 0, RTTHREADTYPE_IO, VIRTQNAME(uVirtqNbr));
2571 if (rc != VINF_SUCCESS)
2572 {
2573 LogRel(("Error creating thread for Virtual Virtq %s: %Rrc\n", VIRTQNAME(uVirtqNbr), rc));
2574 return rc;
2575 }
2576
2577 rc = PDMDevHlpSUPSemEventCreate(pDevIns, &pThis->aWorkers[uVirtqNbr].hEvtProcess);
2578 if (RT_FAILURE(rc))
2579 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2580 N_("DevVirtioSCSI: Failed to create SUP event semaphore"));
2581 }
2582 pThis->afVirtqAttached[uVirtqNbr] = true;
2583 rc = RTCritSectInit(&pThisCC->aWorkers[uVirtqNbr].CritSectVirtq);
2584 if (RT_FAILURE(rc))
2585 return PDMDevHlpVMSetError(pDevIns, rc, RT_SRC_POS,
2586 N_("DevVirtioSCSI: Failed to create worker critical section"));
2587 }
2588
2589 /*
2590 * Initialize per device instances (targets).
2591 */
2592 Log2Func(("Probing %d targets ...\n", pThis->cTargets));
2593
2594 pThisCC->paTargetInstances = (PVIRTIOSCSITARGET)RTMemAllocZ(sizeof(VIRTIOSCSITARGET) * pThis->cTargets);
2595 if (!pThisCC->paTargetInstances)
2596 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to allocate memory for target states"));
2597
2598 for (uint32_t uTarget = 0; uTarget < pThis->cTargets; uTarget++)
2599 {
2600 PVIRTIOSCSITARGET pTarget = &pThisCC->paTargetInstances[uTarget];
2601
2602 if (RTStrAPrintf(&pTarget->pszTargetName, "VSCSI%u", uTarget) < 0)
2603 AssertLogRelFailedReturn(VERR_NO_MEMORY);
2604
2605 /* Initialize static parts of the device. */
2606 pTarget->pDevIns = pDevIns;
2607 pTarget->uTarget = uTarget;
2608
2609 pTarget->IBase.pfnQueryInterface = virtioScsiR3TargetQueryInterface;
2610
2611 /* IMediaPort and IMediaExPort interfaces provide callbacks for VD media and downstream driver access */
2612 pTarget->IMediaPort.pfnQueryDeviceLocation = virtioScsiR3QueryDeviceLocation;
2613 pTarget->IMediaPort.pfnQueryScsiInqStrings = NULL;
2614 pTarget->IMediaExPort.pfnIoReqCompleteNotify = virtioScsiR3IoReqFinish;
2615 pTarget->IMediaExPort.pfnIoReqCopyFromBuf = virtioScsiR3IoReqCopyFromBuf;
2616 pTarget->IMediaExPort.pfnIoReqCopyToBuf = virtioScsiR3IoReqCopyToBuf;
2617 pTarget->IMediaExPort.pfnIoReqStateChanged = virtioScsiR3IoReqStateChanged;
2618 pTarget->IMediaExPort.pfnMediumEjected = virtioScsiR3MediumEjected;
2619 pTarget->IMediaExPort.pfnIoReqQueryBuf = NULL; /* When used avoids copyFromBuf CopyToBuf*/
2620 pTarget->IMediaExPort.pfnIoReqQueryDiscardRanges = NULL;
2621
2622 pTarget->IBase.pfnQueryInterface = virtioScsiR3TargetQueryInterface;
2623 pTarget->ILed.pfnQueryStatusLed = virtioScsiR3TargetQueryStatusLed;
2624 pTarget->led.u32Magic = PDMLED_MAGIC;
2625
2626 LogFunc(("Attaching LUN: %s\n", pTarget->pszTargetName));
2627
2628 AssertReturn(uTarget < pThis->cTargets, VERR_PDM_NO_SUCH_LUN);
2629 rc = PDMDevHlpDriverAttach(pDevIns, uTarget, &pTarget->IBase, &pTarget->pDrvBase, pTarget->pszTargetName);
2630 if (RT_SUCCESS(rc))
2631 {
2632 pTarget->fPresent = true;
2633
2634 pTarget->pDrvMedia = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIA);
2635 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMedia),
2636 ("virtio-scsi configuration error: LUN#%d missing basic media interface!\n", uTarget),
2637 VERR_PDM_MISSING_INTERFACE);
2638 /* Get the extended media interface. */
2639 pTarget->pDrvMediaEx = PDMIBASE_QUERY_INTERFACE(pTarget->pDrvBase, PDMIMEDIAEX);
2640 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMediaEx),
2641 ("virtio-scsi configuration error: LUN#%d missing extended media interface!\n", uTarget),
2642 VERR_PDM_MISSING_INTERFACE);
2643
2644 rc = pTarget->pDrvMediaEx->pfnIoReqAllocSizeSet(pTarget->pDrvMediaEx, sizeof(VIRTIOSCSIREQ));
2645 AssertMsgReturn(RT_VALID_PTR(pTarget->pDrvMediaEx),
2646 ("virtio-scsi configuration error: LUN#%u: Failed to set I/O request size!\n", uTarget),
2647 rc);
2648 }
2649 else if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
2650 {
2651 pTarget->fPresent = false;
2652 pTarget->pDrvBase = NULL;
2653 Log(("virtio-scsi: no driver attached to device %s\n", pTarget->pszTargetName));
2654 rc = VINF_SUCCESS;
2655 }
2656 else
2657 {
2658 AssertLogRelMsgFailed(("virtio-scsi: Failed to attach %s: %Rrc\n", pTarget->pszTargetName, rc));
2659 return rc;
2660 }
2661 }
2662
2663 /*
2664 * Status driver (optional).
2665 */
2666 PPDMIBASE pUpBase = NULL;
2667 AssertCompile(PDM_STATUS_LUN >= VIRTIOSCSI_MAX_TARGETS);
2668 rc = PDMDevHlpDriverAttach(pDevIns, PDM_STATUS_LUN, &pThisCC->IBase, &pUpBase, "Status Port");
2669 if (RT_FAILURE(rc) && rc != VERR_PDM_NO_ATTACHED_DRIVER)
2670 return PDMDEV_SET_ERROR(pDevIns, rc, N_("Failed to attach the status LUN"));
2671 if (RT_SUCCESS(rc) && pUpBase)
2672 pThisCC->pMediaNotify = PDMIBASE_QUERY_INTERFACE(pUpBase, PDMIMEDIANOTIFY);
2673
2674
2675 /*
2676 * Register saved state.
2677 */
2678 rc = PDMDevHlpSSMRegister(pDevIns, VIRTIOSCSI_SAVED_STATE_VERSION, sizeof(*pThis),
2679 virtioScsiR3SaveExec, virtioScsiR3LoadExec);
2680 AssertRCReturn(rc, rc);
2681
2682 /*
2683 * Register the debugger info callback (ignore errors).
2684 */
2685 char szTmp[128];
2686 RTStrPrintf(szTmp, sizeof(szTmp), "%s%u", pDevIns->pReg->szName, pDevIns->iInstance);
2687 PDMDevHlpDBGFInfoRegister(pDevIns, szTmp, "virtio-scsi info", virtioScsiR3Info);
2688
2689 return rc;
2690}
2691
2692#else /* !IN_RING3 */
2693
2694/**
2695 * @callback_method_impl{PDMDEVREGR0,pfnConstruct}
2696 */
2697static DECLCALLBACK(int) virtioScsiRZConstruct(PPDMDEVINS pDevIns)
2698{
2699 PDMDEV_CHECK_VERSIONS_RETURN(pDevIns);
2700
2701 PVIRTIOSCSI pThis = PDMDEVINS_2_DATA(pDevIns, PVIRTIOSCSI);
2702 PVIRTIOSCSICC pThisCC = PDMDEVINS_2_DATA_CC(pDevIns, PVIRTIOSCSICC);
2703
2704
2705 pThisCC->Virtio.pfnVirtqNotified = virtioScsiNotified;
2706 return virtioCoreRZInit(pDevIns, &pThis->Virtio);
2707}
2708
2709#endif /* !IN_RING3 */
2710
2711
2712/**
2713 * The device registration structure.
2714 */
2715const PDMDEVREG g_DeviceVirtioSCSI =
2716{
2717 /* .u32Version = */ PDM_DEVREG_VERSION,
2718 /* .uReserved0 = */ 0,
2719 /* .szName = */ "virtio-scsi",
2720 /* .fFlags = */ PDM_DEVREG_FLAGS_DEFAULT_BITS | PDM_DEVREG_FLAGS_RZ | PDM_DEVREG_FLAGS_NEW_STYLE
2721 | PDM_DEVREG_FLAGS_FIRST_SUSPEND_NOTIFICATION
2722 | PDM_DEVREG_FLAGS_FIRST_POWEROFF_NOTIFICATION,
2723 /* .fClass = */ PDM_DEVREG_CLASS_STORAGE,
2724 /* .cMaxInstances = */ ~0U,
2725 /* .uSharedVersion = */ 42,
2726 /* .cbInstanceShared = */ sizeof(VIRTIOSCSI),
2727 /* .cbInstanceCC = */ sizeof(VIRTIOSCSICC),
2728 /* .cbInstanceRC = */ sizeof(VIRTIOSCSIRC),
2729 /* .cMaxPciDevices = */ 1,
2730 /* .cMaxMsixVectors = */ VBOX_MSIX_MAX_ENTRIES,
2731 /* .pszDescription = */ "Virtio Host SCSI.\n",
2732#if defined(IN_RING3)
2733 /* .pszRCMod = */ "VBoxDDRC.rc",
2734 /* .pszR0Mod = */ "VBoxDDR0.r0",
2735 /* .pfnConstruct = */ virtioScsiR3Construct,
2736 /* .pfnDestruct = */ virtioScsiR3Destruct,
2737 /* .pfnRelocate = */ NULL,
2738 /* .pfnMemSetup = */ NULL,
2739 /* .pfnPowerOn = */ NULL,
2740 /* .pfnReset = */ virtioScsiR3Reset,
2741 /* .pfnSuspend = */ virtioScsiR3Suspend,
2742 /* .pfnResume = */ virtioScsiR3Resume,
2743 /* .pfnAttach = */ virtioScsiR3Attach,
2744 /* .pfnDetach = */ virtioScsiR3Detach,
2745 /* .pfnQueryInterface = */ NULL,
2746 /* .pfnInitComplete = */ NULL,
2747 /* .pfnPowerOff = */ virtioScsiR3PowerOff,
2748 /* .pfnSoftReset = */ NULL,
2749 /* .pfnReserved0 = */ NULL,
2750 /* .pfnReserved1 = */ NULL,
2751 /* .pfnReserved2 = */ NULL,
2752 /* .pfnReserved3 = */ NULL,
2753 /* .pfnReserved4 = */ NULL,
2754 /* .pfnReserved5 = */ NULL,
2755 /* .pfnReserved6 = */ NULL,
2756 /* .pfnReserved7 = */ NULL,
2757#elif defined(IN_RING0)
2758 /* .pfnEarlyConstruct = */ NULL,
2759 /* .pfnConstruct = */ virtioScsiRZConstruct,
2760 /* .pfnDestruct = */ NULL,
2761 /* .pfnFinalDestruct = */ NULL,
2762 /* .pfnRequest = */ NULL,
2763 /* .pfnReserved0 = */ NULL,
2764 /* .pfnReserved1 = */ NULL,
2765 /* .pfnReserved2 = */ NULL,
2766 /* .pfnReserved3 = */ NULL,
2767 /* .pfnReserved4 = */ NULL,
2768 /* .pfnReserved5 = */ NULL,
2769 /* .pfnReserved6 = */ NULL,
2770 /* .pfnReserved7 = */ NULL,
2771#elif defined(IN_RC)
2772 /* .pfnConstruct = */ virtioScsiRZConstruct,
2773 /* .pfnReserved0 = */ NULL,
2774 /* .pfnReserved1 = */ NULL,
2775 /* .pfnReserved2 = */ NULL,
2776 /* .pfnReserved3 = */ NULL,
2777 /* .pfnReserved4 = */ NULL,
2778 /* .pfnReserved5 = */ NULL,
2779 /* .pfnReserved6 = */ NULL,
2780 /* .pfnReserved7 = */ NULL,
2781#else
2782# error "Not in IN_RING3, IN_RING0 or IN_RC!"
2783#endif
2784 /* .u32VersionEnd = */ PDM_DEVREG_VERSION
2785};
2786
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