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