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