1 | /** @file
|
---|
2 |
|
---|
3 | Implementation of the SNP.Transmit() function and its private helpers if any.
|
---|
4 |
|
---|
5 | Copyright (C) 2013, Red Hat, Inc.
|
---|
6 | Copyright (c) 2006 - 2013, Intel Corporation. All rights reserved.<BR>
|
---|
7 |
|
---|
8 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
9 |
|
---|
10 | **/
|
---|
11 |
|
---|
12 | #include <Library/BaseLib.h>
|
---|
13 | #include <Library/BaseMemoryLib.h>
|
---|
14 | #include <Library/UefiBootServicesTableLib.h>
|
---|
15 |
|
---|
16 | #include "VirtioNet.h"
|
---|
17 |
|
---|
18 | /**
|
---|
19 | Places a packet in the transmit queue of a network interface.
|
---|
20 |
|
---|
21 | @param This The protocol instance pointer.
|
---|
22 | @param HeaderSize The size, in bytes, of the media header to be filled in by
|
---|
23 | the Transmit() function. If HeaderSize is non-zero, then
|
---|
24 | it must be equal to This->Mode->MediaHeaderSize and the
|
---|
25 | DestAddr and Protocol parameters must not be NULL.
|
---|
26 | @param BufferSize The size, in bytes, of the entire packet (media header and
|
---|
27 | data) to be transmitted through the network interface.
|
---|
28 | @param Buffer A pointer to the packet (media header followed by data) to
|
---|
29 | be transmitted. This parameter cannot be NULL. If
|
---|
30 | HeaderSize is zero, then the media header in Buffer must
|
---|
31 | already be filled in by the caller. If HeaderSize is
|
---|
32 | non-zero, then the media header will be filled in by the
|
---|
33 | Transmit() function.
|
---|
34 | @param SrcAddr The source HW MAC address. If HeaderSize is zero, then
|
---|
35 | this parameter is ignored. If HeaderSize is non-zero and
|
---|
36 | SrcAddr is NULL, then This->Mode->CurrentAddress is used
|
---|
37 | for the source HW MAC address.
|
---|
38 | @param DestAddr The destination HW MAC address. If HeaderSize is zero,
|
---|
39 | then this parameter is ignored.
|
---|
40 | @param Protocol The type of header to build. If HeaderSize is zero, then
|
---|
41 | this parameter is ignored. See RFC 1700, section "Ether
|
---|
42 | Types", for examples.
|
---|
43 |
|
---|
44 | @retval EFI_SUCCESS The packet was placed on the transmit queue.
|
---|
45 | @retval EFI_NOT_STARTED The network interface has not been started.
|
---|
46 | @retval EFI_NOT_READY The network interface is too busy to accept
|
---|
47 | this transmit request.
|
---|
48 | @retval EFI_BUFFER_TOO_SMALL The BufferSize parameter is too small.
|
---|
49 | @retval EFI_INVALID_PARAMETER One or more of the parameters has an
|
---|
50 | unsupported value.
|
---|
51 | @retval EFI_DEVICE_ERROR The command could not be sent to the network
|
---|
52 | interface.
|
---|
53 | @retval EFI_UNSUPPORTED This function is not supported by the network
|
---|
54 | interface.
|
---|
55 |
|
---|
56 | **/
|
---|
57 | EFI_STATUS
|
---|
58 | EFIAPI
|
---|
59 | VirtioNetTransmit (
|
---|
60 | IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
|
---|
61 | IN UINTN HeaderSize,
|
---|
62 | IN UINTN BufferSize,
|
---|
63 | IN /* +OUT! */ VOID *Buffer,
|
---|
64 | IN EFI_MAC_ADDRESS *SrcAddr OPTIONAL,
|
---|
65 | IN EFI_MAC_ADDRESS *DestAddr OPTIONAL,
|
---|
66 | IN UINT16 *Protocol OPTIONAL
|
---|
67 | )
|
---|
68 | {
|
---|
69 | VNET_DEV *Dev;
|
---|
70 | EFI_TPL OldTpl;
|
---|
71 | EFI_STATUS Status;
|
---|
72 | UINT16 DescIdx;
|
---|
73 | UINT16 AvailIdx;
|
---|
74 | EFI_PHYSICAL_ADDRESS DeviceAddress;
|
---|
75 |
|
---|
76 | if ((This == NULL) || (BufferSize == 0) || (Buffer == NULL)) {
|
---|
77 | return EFI_INVALID_PARAMETER;
|
---|
78 | }
|
---|
79 |
|
---|
80 | Dev = VIRTIO_NET_FROM_SNP (This);
|
---|
81 | OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
|
---|
82 | switch (Dev->Snm.State) {
|
---|
83 | case EfiSimpleNetworkStopped:
|
---|
84 | Status = EFI_NOT_STARTED;
|
---|
85 | goto Exit;
|
---|
86 | case EfiSimpleNetworkStarted:
|
---|
87 | Status = EFI_DEVICE_ERROR;
|
---|
88 | goto Exit;
|
---|
89 | default:
|
---|
90 | break;
|
---|
91 | }
|
---|
92 |
|
---|
93 | if (BufferSize < Dev->Snm.MediaHeaderSize) {
|
---|
94 | Status = EFI_BUFFER_TOO_SMALL;
|
---|
95 | goto Exit;
|
---|
96 | }
|
---|
97 |
|
---|
98 | if (BufferSize > Dev->Snm.MediaHeaderSize + Dev->Snm.MaxPacketSize) {
|
---|
99 | Status = EFI_INVALID_PARAMETER;
|
---|
100 | goto Exit;
|
---|
101 | }
|
---|
102 |
|
---|
103 | //
|
---|
104 | // check if we have room for transmission
|
---|
105 | //
|
---|
106 | ASSERT (Dev->TxCurPending <= Dev->TxMaxPending);
|
---|
107 | if (Dev->TxCurPending == Dev->TxMaxPending) {
|
---|
108 | Status = EFI_NOT_READY;
|
---|
109 | goto Exit;
|
---|
110 | }
|
---|
111 |
|
---|
112 | //
|
---|
113 | // the caller may want us to fill in the media header:
|
---|
114 | // dst MAC, src MAC, Ethertype
|
---|
115 | //
|
---|
116 | if (HeaderSize != 0) {
|
---|
117 | UINT8 *Ptr;
|
---|
118 |
|
---|
119 | if ((HeaderSize != Dev->Snm.MediaHeaderSize) ||
|
---|
120 | (DestAddr == NULL) || (Protocol == NULL))
|
---|
121 | {
|
---|
122 | Status = EFI_INVALID_PARAMETER;
|
---|
123 | goto Exit;
|
---|
124 | }
|
---|
125 |
|
---|
126 | Ptr = Buffer;
|
---|
127 | ASSERT (SIZE_OF_VNET (Mac) <= sizeof (EFI_MAC_ADDRESS));
|
---|
128 |
|
---|
129 | CopyMem (Ptr, DestAddr, SIZE_OF_VNET (Mac));
|
---|
130 | Ptr += SIZE_OF_VNET (Mac);
|
---|
131 |
|
---|
132 | CopyMem (
|
---|
133 | Ptr,
|
---|
134 | (SrcAddr == NULL) ? &Dev->Snm.CurrentAddress : SrcAddr,
|
---|
135 | SIZE_OF_VNET (Mac)
|
---|
136 | );
|
---|
137 | Ptr += SIZE_OF_VNET (Mac);
|
---|
138 |
|
---|
139 | *Ptr++ = (UINT8)(*Protocol >> 8);
|
---|
140 | *Ptr++ = (UINT8)*Protocol;
|
---|
141 |
|
---|
142 | ASSERT ((UINTN)(Ptr - (UINT8 *)Buffer) == Dev->Snm.MediaHeaderSize);
|
---|
143 | }
|
---|
144 |
|
---|
145 | //
|
---|
146 | // Map the transmit buffer system physical address to device address.
|
---|
147 | //
|
---|
148 | Status = VirtioNetMapTxBuf (
|
---|
149 | Dev,
|
---|
150 | Buffer,
|
---|
151 | BufferSize,
|
---|
152 | &DeviceAddress
|
---|
153 | );
|
---|
154 | if (EFI_ERROR (Status)) {
|
---|
155 | Status = EFI_DEVICE_ERROR;
|
---|
156 | goto Exit;
|
---|
157 | }
|
---|
158 |
|
---|
159 | //
|
---|
160 | // virtio-0.9.5, 2.4.1 Supplying Buffers to The Device
|
---|
161 | //
|
---|
162 | DescIdx = Dev->TxFreeStack[Dev->TxCurPending++];
|
---|
163 | Dev->TxRing.Desc[DescIdx + 1].Addr = DeviceAddress;
|
---|
164 | Dev->TxRing.Desc[DescIdx + 1].Len = (UINT32)BufferSize;
|
---|
165 |
|
---|
166 | //
|
---|
167 | // the available index is never written by the host, we can read it back
|
---|
168 | // without a barrier
|
---|
169 | //
|
---|
170 | AvailIdx = *Dev->TxRing.Avail.Idx;
|
---|
171 | Dev->TxRing.Avail.Ring[AvailIdx++ % Dev->TxRing.QueueSize] = DescIdx;
|
---|
172 |
|
---|
173 | MemoryFence ();
|
---|
174 | *Dev->TxRing.Avail.Idx = AvailIdx;
|
---|
175 |
|
---|
176 | MemoryFence ();
|
---|
177 | Status = Dev->VirtIo->SetQueueNotify (Dev->VirtIo, VIRTIO_NET_Q_TX);
|
---|
178 |
|
---|
179 | Exit:
|
---|
180 | gBS->RestoreTPL (OldTpl);
|
---|
181 | return Status;
|
---|
182 | }
|
---|