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