VirtualBox

source: vbox/trunk/src/VBox/Additions/solaris/Virtio/VirtioRing-solaris.c@ 104448

Last change on this file since 104448 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.2 KB
Line 
1/* $Id: VirtioRing-solaris.c 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions: Virtio Driver for Solaris, Ring implementation.
4 */
5
6/*
7 * Copyright (C) 2010-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "Virtio-solaris.h"
42
43#include <iprt/asm.h>
44#include <iprt/cdefs.h>
45#include <iprt/assert.h>
46#include <iprt/errcore.h>
47#include <iprt/log.h>
48
49#include <sys/cmn_err.h>
50
51/**
52 * Returns the size of the ring in bytes given the number of elements and
53 * alignment requirements.
54 *
55 * @param cElements Number of elements.
56 * @param Align Alignment (must be a power of two).
57 *
58 * @return Size of the Virtio ring.
59 */
60size_t VirtioRingSize(uint64_t cElements, ulong_t Align)
61{
62 size_t cb = 0;
63 cb = cElements * sizeof(VIRTIORINGDESC); /* Ring descriptors. */
64 cb += 2 * sizeof(uint16_t); /* Available flags and index. */
65 cb += cElements * sizeof(uint16_t); /* Available descriptors. */
66
67 size_t cbAlign = RT_ALIGN_Z(cb, Align);
68 cbAlign += 2 * sizeof(uint16_t); /* Used flags and index. */
69 cbAlign += cElements * sizeof(VIRTIORINGUSEDELEM); /* Used descriptors. */
70
71 return cbAlign;
72}
73
74
75/**
76 * Initializes a ring of a queue. This associates the DMA virtual address
77 * with the Ring structure's "pRingDesc".
78 *
79 * @param pQueue Pointer to the Virtio Queue.
80 * @param cDescs Number of descriptors.
81 * @param virtBuf Buffer associated with the ring.
82 * @param Align Alignment (must be power of two).
83 */
84void VirtioRingInit(PVIRTIOQUEUE pQueue, uint_t cDescs, caddr_t virtBuf, ulong_t Align)
85{
86 PVIRTIORING pRing = &pQueue->Ring;
87 pRing->cDesc = cDescs;
88 pRing->pRingDesc = (void *)virtBuf;
89 pRing->pRingAvail = (PVIRTIORINGAVAIL)(virtBuf + (cDescs * sizeof(pRing->pRingDesc[0])));
90 pRing->pRingUsedElem = RT_ALIGN_PT(pRing->pRingAvail + RT_UOFFSETOF_DYN(VIRTIORINGAVAIL, aRings[pQueue->Ring.cDesc]), Align,
91 PVIRTIORINGUSEDELEM);
92
93 for (uint_t i = 0; i < pRing->cDesc - 1; i++)
94 pRing->pRingDesc[i].Next = i + 1;
95
96 pQueue->FreeHeadIndex = 0;
97
98 cmn_err(CE_NOTE, "cDesc=%u pRingDesc=%p pRingAvail=%p\n", pRing->cDesc, pRing->pRingDesc, pRing->pRingAvail);
99}
100
101
102/**
103 * Push a buffer into the ring.
104 *
105 * @param pQueue Pointer to the Virtio queue.
106 * @param physBuf Physical address of the buffer.
107 * @param cbBuf Size of the buffer in bytes.
108 * @param fFlags Buffer flags, see VIRTIO_FLAGS_RING_DESC_*.
109 *
110 * @return IPRT error code.
111 */
112int VirtioRingPush(PVIRTIOQUEUE pQueue, paddr_t physBuf, uint32_t cbBuf, uint16_t fFlags)
113{
114 /*
115 * Claim a slot, fill the buffer and move head pointer.
116 */
117 uint_t FreeIndex = pQueue->FreeHeadIndex;
118 PVIRTIORING pRing = &pQueue->Ring;
119
120 if (FreeIndex >= pRing->cDesc - 1)
121 {
122 LogRel((VIRTIOLOGNAME ":VirtioRingPush: failed. No free descriptors. cDesc=%u\n", pRing->cDesc));
123 return VERR_BUFFER_OVERFLOW;
124 }
125
126 PVIRTIORINGDESC pRingDesc = &pRing->pRingDesc[FreeIndex];
127
128 AssertCompile(sizeof(physBuf) == sizeof(pRingDesc->AddrBuf));
129
130 pQueue->cBufs++;
131 uint_t AvailIndex = (pRing->pRingAvail->Index + pQueue->cBufs) % pQueue->Ring.cDesc;
132 pRing->pRingAvail->aRings[AvailIndex - 1] = FreeIndex;
133
134 pRingDesc->AddrBuf = physBuf;
135 pRingDesc->cbBuf = cbBuf;
136 pRingDesc->fFlags = fFlags;
137
138 pQueue->FreeHeadIndex = pRingDesc->Next;
139
140 ASMCompilerBarrier();
141
142 cmn_err(CE_NOTE, "VirtioRingPush: cbBuf=%u FreeIndex=%u AvailIndex=%u cDesc=%u Queue->cBufs=%u\n",
143 cbBuf, FreeIndex, AvailIndex, pQueue->Ring.cDesc,
144 pQueue->cBufs);
145
146 return VINF_SUCCESS;
147}
148
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