iPXE
Data Structures | Macros | Enumerations | Functions | Variables
virtio-net.c File Reference
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <ipxe/list.h>
#include <ipxe/iobuf.h>
#include <ipxe/netdevice.h>
#include <ipxe/pci.h>
#include <ipxe/if_ether.h>
#include <ipxe/ethernet.h>
#include <ipxe/virtio-pci.h>
#include <ipxe/virtio-ring.h>
#include "virtio-net.h"

Go to the source code of this file.

Data Structures

struct  virtnet_nic
 

Macros

#define NUM_RX_BUF   8
 Max number of pending rx packets. More...
 

Enumerations

enum  { RX_INDEX = 0, TX_INDEX, QUEUE_NB }
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
static void virtnet_enqueue_iob (struct net_device *netdev, int vq_idx, struct io_buffer *iobuf)
 Add an iobuf to a virtqueue. More...
 
static void virtnet_refill_rx_virtqueue (struct net_device *netdev)
 Try to keep rx virtqueue filled with iobufs. More...
 
static void virtnet_free_virtqueues (struct net_device *netdev)
 Helper to free all virtqueue memory. More...
 
static int virtnet_open_legacy (struct net_device *netdev)
 Open network device, legacy virtio 0.9.5. More...
 
static int virtnet_open_modern (struct net_device *netdev)
 Open network device, modern virtio 1.0. More...
 
static int virtnet_open (struct net_device *netdev)
 Open network device. More...
 
static void virtnet_close (struct net_device *netdev)
 Close network device. More...
 
static int virtnet_transmit (struct net_device *netdev, struct io_buffer *iobuf)
 Transmit packet. More...
 
static void virtnet_process_tx_packets (struct net_device *netdev)
 Complete packet transmission. More...
 
static void virtnet_process_rx_packets (struct net_device *netdev)
 Complete packet reception. More...
 
static void virtnet_poll (struct net_device *netdev)
 Poll for completed and received packets. More...
 
static void virtnet_irq (struct net_device *netdev, int enable)
 Enable or disable interrupts. More...
 
static int virtnet_probe_legacy (struct pci_device *pci)
 Probe PCI device, legacy virtio 0.9.5. More...
 
static int virtnet_probe_modern (struct pci_device *pci, int *found_dev)
 Probe PCI device, modern virtio 1.0. More...
 
static int virtnet_probe (struct pci_device *pci)
 Probe PCI device. More...
 
static void virtnet_remove (struct pci_device *pci)
 Remove device. More...
 

Variables

static struct net_device_operations virtnet_operations
 virtio-net device operations More...
 
static struct pci_device_id virtnet_nics []
 
struct pci_driver virtnet_driver __pci_driver
 

Macro Definition Documentation

◆ NUM_RX_BUF

#define NUM_RX_BUF   8

Max number of pending rx packets.

Definition at line 81 of file virtio-net.c.

Enumeration Type Documentation

◆ anonymous enum

anonymous enum
Enumerator
RX_INDEX 
TX_INDEX 
QUEUE_NB 

Definition at line 74 of file virtio-net.c.

74  {
75  RX_INDEX = 0,
76  TX_INDEX,
77  QUEUE_NB
78 };

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ virtnet_enqueue_iob()

static void virtnet_enqueue_iob ( struct net_device netdev,
int  vq_idx,
struct io_buffer iobuf 
)
static

Add an iobuf to a virtqueue.

Parameters
netdevNetwork device
vq_idxVirtqueue index (RX_INDEX or TX_INDEX)
iobufI/O buffer

The virtqueue is kicked after the iobuf has been added.

Definition at line 114 of file virtio-net.c.

115  {
116  struct virtnet_nic *virtnet = netdev->priv;
117  struct vring_virtqueue *vq = &virtnet->virtqueue[vq_idx];
118  struct virtio_net_hdr_modern *header = &virtnet->empty_header[vq_idx];
119  unsigned int out = ( vq_idx == TX_INDEX ) ? 2 : 0;
120  unsigned int in = ( vq_idx == TX_INDEX ) ? 0 : 2;
121  size_t header_len = ( virtnet->virtio_version ?
122  sizeof ( *header ) : sizeof ( header->legacy ) );
123  struct vring_list list[] = {
124  {
125  /* Share a single zeroed virtio net header between all
126  * packets in a ring. This works because this driver
127  * does not use any advanced features so none of the
128  * header fields get used.
129  *
130  * Some host implementations (notably Google Compute
131  * Platform) are known to unconditionally write back
132  * to header->flags for received packets. Work around
133  * this by using separate RX and TX headers.
134  */
135  .addr = ( char* ) header,
136  .length = header_len,
137  },
138  {
139  .addr = ( char* ) iobuf->data,
140  .length = iob_len ( iobuf ),
141  },
142  };
143 
144  DBGC2 ( virtnet, "VIRTIO-NET %p enqueuing iobuf %p on vq %d\n",
145  virtnet, iobuf, vq_idx );
146 
147  vring_add_buf ( vq, list, out, in, iobuf, 0 );
148  vring_kick ( virtnet->virtio_version ? &virtnet->vdev : NULL,
149  virtnet->ioaddr, vq, 1 );
150 }
void vring_kick(struct virtio_pci_modern_device *vdev, unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
Definition: virtio-ring.c:125
uint16_t length
Length.
Definition: intel.h:14
struct virtio_net_hdr_modern empty_header[QUEUE_NB]
Virtio net dummy packet headers.
Definition: virtio-net.c:103
__be32 in[4]
Definition: CIB_PRM.h:35
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
__be32 out[4]
Definition: CIB_PRM.h:36
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:88
#define DBGC2(...)
Definition: compiler.h:522
void * data
Start of data.
Definition: iobuf.h:44
struct ena_aq_header header
Header.
Definition: ena.h:12
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:85
void vring_add_buf(struct vring_virtqueue *vq, struct vring_list list[], unsigned int out, unsigned int in, void *opaque, int num_added)
Definition: virtio-ring.c:86
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
char * addr
Definition: virtio-ring.h:87
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94

References vring_list::addr, io_buffer::data, DBGC2, virtnet_nic::empty_header, header, in, virtnet_nic::ioaddr, iob_len(), length, netdev, NULL, out, net_device::priv, TX_INDEX, virtnet_nic::vdev, virtnet_nic::virtio_version, virtnet_nic::virtqueue, vring_add_buf(), and vring_kick().

Referenced by virtnet_refill_rx_virtqueue(), and virtnet_transmit().

◆ virtnet_refill_rx_virtqueue()

static void virtnet_refill_rx_virtqueue ( struct net_device netdev)
static

Try to keep rx virtqueue filled with iobufs.

Parameters
netdevNetwork device

Definition at line 156 of file virtio-net.c.

156  {
157  struct virtnet_nic *virtnet = netdev->priv;
158  size_t len = ( netdev->max_pkt_len + 4 /* VLAN */ );
159 
160  while ( virtnet->rx_num_iobufs < NUM_RX_BUF ) {
161  struct io_buffer *iobuf;
162 
163  /* Try to allocate a buffer, stop for now if out of memory */
164  iobuf = alloc_iob ( len );
165  if ( ! iobuf )
166  break;
167 
168  /* Keep track of iobuf so close() can free it */
169  list_add ( &iobuf->list, &virtnet->rx_iobufs );
170 
171  /* Mark packet length until we know the actual size */
172  iob_put ( iobuf, len );
173 
174  virtnet_enqueue_iob ( netdev, RX_INDEX, iobuf );
175  virtnet->rx_num_iobufs++;
176  }
177 }
#define iob_put(iobuf, len)
Definition: iobuf.h:116
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
struct io_buffer * alloc_iob(size_t len)
Allocate I/O buffer.
Definition: iobuf.c:128
static void virtnet_enqueue_iob(struct net_device *netdev, int vq_idx, struct io_buffer *iobuf)
Add an iobuf to a virtqueue.
Definition: virtio-net.c:114
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:100
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:39
#define NUM_RX_BUF
Max number of pending rx packets.
Definition: virtio-net.c:81
uint32_t len
Length.
Definition: ena.h:14
struct list_head rx_iobufs
RX packets handed to the NIC waiting to be filled in.
Definition: virtio-net.c:97
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:403
A persistent I/O buffer.
Definition: iobuf.h:32

References alloc_iob(), iob_put, len, io_buffer::list, list_add, net_device::max_pkt_len, netdev, NUM_RX_BUF, net_device::priv, RX_INDEX, virtnet_nic::rx_iobufs, virtnet_nic::rx_num_iobufs, and virtnet_enqueue_iob().

Referenced by virtnet_open_legacy(), virtnet_open_modern(), and virtnet_process_rx_packets().

◆ virtnet_free_virtqueues()

static void virtnet_free_virtqueues ( struct net_device netdev)
static

Helper to free all virtqueue memory.

Parameters
netdevNetwork device

Definition at line 183 of file virtio-net.c.

183  {
184  struct virtnet_nic *virtnet = netdev->priv;
185  int i;
186 
187  for ( i = 0; i < QUEUE_NB; i++ ) {
189  vp_free_vq ( &virtnet->virtqueue[i] );
190  }
191 
192  free ( virtnet->virtqueue );
193  virtnet->virtqueue = NULL;
194 }
void vp_free_vq(struct vring_virtqueue *vq)
Definition: virtio-pci.c:40
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
void virtio_pci_unmap_capability(struct virtio_pci_region *region)
Definition: virtio-pci.c:336
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct virtio_pci_region notification
Definition: virtio-ring.h:83
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94

References free, netdev, vring_virtqueue::notification, NULL, net_device::priv, QUEUE_NB, virtio_pci_unmap_capability(), virtnet_nic::virtqueue, and vp_free_vq().

Referenced by virtnet_close(), virtnet_open_legacy(), and virtnet_open_modern().

◆ virtnet_open_legacy()

static int virtnet_open_legacy ( struct net_device netdev)
static

Open network device, legacy virtio 0.9.5.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 201 of file virtio-net.c.

201  {
202  struct virtnet_nic *virtnet = netdev->priv;
203  unsigned long ioaddr = virtnet->ioaddr;
204  u32 features;
205  int i;
206 
207  /* Reset for sanity */
208  vp_reset ( ioaddr );
209 
210  /* Allocate virtqueues */
211  virtnet->virtqueue = zalloc ( QUEUE_NB *
212  sizeof ( *virtnet->virtqueue ) );
213  if ( ! virtnet->virtqueue )
214  return -ENOMEM;
215 
216  /* Initialize rx/tx virtqueues */
217  for ( i = 0; i < QUEUE_NB; i++ ) {
218  if ( vp_find_vq ( ioaddr, i, &virtnet->virtqueue[i] ) == -1 ) {
219  DBGC ( virtnet, "VIRTIO-NET %p cannot register queue %d\n",
220  virtnet, i );
222  return -ENOENT;
223  }
224  }
225 
226  /* Initialize rx packets */
227  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
228  virtnet->rx_num_iobufs = 0;
230 
231  /* Disable interrupts before starting */
232  netdev_irq ( netdev, 0 );
233 
234  /* Driver is ready */
237  ( 1 << VIRTIO_NET_F_MTU ) ) );
239  return 0;
240 }
static void virtnet_free_virtqueues(struct net_device *netdev)
Helper to free all virtqueue memory.
Definition: virtio-net.c:183
static u32 vp_get_features(unsigned int ioaddr)
Definition: virtio-pci.h:138
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define VIRTIO_NET_F_MTU
Definition: virtio-net.h:7
static void vp_set_features(unsigned int ioaddr, u32 features)
Definition: virtio-pci.h:143
#define ENOMEM
Not enough space.
Definition: errno.h:534
static unsigned long ioaddr
Definition: davicom.c:129
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:100
static void vp_set_status(unsigned int ioaddr, u8 status)
Definition: virtio-pci.h:163
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
uint32_t features
Supported features.
Definition: ena.h:16
void netdev_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: netdevice.c:875
#define VIRTIO_NET_F_MAC
Definition: virtio-net.h:8
#define VIRTIO_CONFIG_S_DRIVER
Definition: virtio-ring.h:10
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
struct list_head rx_iobufs
RX packets handed to the NIC waiting to be filled in.
Definition: virtio-net.c:97
int vp_find_vq(unsigned int ioaddr, int queue_index, struct vring_virtqueue *vq)
Definition: virtio-pci.c:49
static void virtnet_refill_rx_virtqueue(struct net_device *netdev)
Try to keep rx virtqueue filled with iobufs.
Definition: virtio-net.c:156
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:85
#define VIRTIO_CONFIG_S_DRIVER_OK
Definition: virtio-ring.h:12
static void vp_reset(unsigned int ioaddr)
Definition: virtio-pci.h:175
uint32_t u32
Definition: stdint.h:23
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94

References DBGC, ENOENT, ENOMEM, features, INIT_LIST_HEAD, virtnet_nic::ioaddr, ioaddr, netdev, netdev_irq(), net_device::priv, QUEUE_NB, virtnet_nic::rx_iobufs, virtnet_nic::rx_num_iobufs, VIRTIO_CONFIG_S_DRIVER, VIRTIO_CONFIG_S_DRIVER_OK, VIRTIO_NET_F_MAC, VIRTIO_NET_F_MTU, virtnet_free_virtqueues(), virtnet_refill_rx_virtqueue(), virtnet_nic::virtqueue, vp_find_vq(), vp_get_features(), vp_reset(), vp_set_features(), vp_set_status(), and zalloc().

Referenced by virtnet_open().

◆ virtnet_open_modern()

static int virtnet_open_modern ( struct net_device netdev)
static

Open network device, modern virtio 1.0.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 247 of file virtio-net.c.

247  {
248  struct virtnet_nic *virtnet = netdev->priv;
249  u64 features;
250  u8 status;
251 
252  /* Negotiate features */
253  features = vpm_get_features ( &virtnet->vdev );
254  if ( ! ( features & VIRTIO_F_VERSION_1 ) ) {
256  return -EINVAL;
257  }
258  vpm_set_features ( &virtnet->vdev, features & (
259  ( 1ULL << VIRTIO_NET_F_MAC ) |
260  ( 1ULL << VIRTIO_NET_F_MTU ) |
261  ( 1ULL << VIRTIO_F_VERSION_1 ) |
262  ( 1ULL << VIRTIO_F_ANY_LAYOUT ) |
263  ( 1ULL << VIRTIO_F_IOMMU_PLATFORM ) ) );
265 
266  status = vpm_get_status ( &virtnet->vdev );
267  if ( ! ( status & VIRTIO_CONFIG_S_FEATURES_OK ) ) {
268  DBGC ( virtnet, "VIRTIO-NET %p device didn't accept features\n",
269  virtnet );
271  return -EINVAL;
272  }
273 
274  /* Allocate virtqueues */
275  virtnet->virtqueue = zalloc ( QUEUE_NB *
276  sizeof ( *virtnet->virtqueue ) );
277  if ( ! virtnet->virtqueue ) {
279  return -ENOMEM;
280  }
281 
282  /* Initialize rx/tx virtqueues */
283  if ( vpm_find_vqs ( &virtnet->vdev, QUEUE_NB, virtnet->virtqueue ) ) {
284  DBGC ( virtnet, "VIRTIO-NET %p cannot register queues\n",
285  virtnet );
288  return -ENOENT;
289  }
290 
291  /* Disable interrupts before starting */
292  netdev_irq ( netdev, 0 );
293 
295 
296  /* Initialize rx packets */
297  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
298  virtnet->rx_num_iobufs = 0;
300  return 0;
301 }
#define VIRTIO_F_VERSION_1
Definition: virtio-ring.h:22
static u8 vpm_get_status(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:245
static void vpm_set_features(struct virtio_pci_modern_device *vdev, u64 features)
Definition: virtio-pci.h:270
#define EINVAL
Invalid argument.
Definition: errno.h:428
static void virtnet_free_virtqueues(struct net_device *netdev)
Helper to free all virtqueue memory.
Definition: virtio-net.c:183
#define DBGC(...)
Definition: compiler.h:505
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define VIRTIO_NET_F_MTU
Definition: virtio-net.h:7
int vpm_find_vqs(struct virtio_pci_modern_device *vdev, unsigned nvqs, struct vring_virtqueue *vqs)
Definition: virtio-pci.c:350
uint8_t status
Status.
Definition: ena.h:16
#define ENOMEM
Not enough space.
Definition: errno.h:534
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
uint64_t u64
Definition: stdint.h:25
#define VIRTIO_F_IOMMU_PLATFORM
Definition: virtio-ring.h:23
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:100
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
uint32_t features
Supported features.
Definition: ena.h:16
void netdev_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: netdevice.c:875
#define VIRTIO_F_ANY_LAYOUT
Definition: virtio-ring.h:20
#define VIRTIO_NET_F_MAC
Definition: virtio-net.h:8
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
static void vpm_add_status(struct virtio_pci_modern_device *vdev, u8 status)
Definition: virtio-pci.h:250
struct list_head rx_iobufs
RX packets handed to the NIC waiting to be filled in.
Definition: virtio-net.c:97
static void virtnet_refill_rx_virtqueue(struct net_device *netdev)
Try to keep rx virtqueue filled with iobufs.
Definition: virtio-net.c:156
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91
#define VIRTIO_CONFIG_S_FAILED
Definition: virtio-ring.h:16
#define VIRTIO_CONFIG_S_DRIVER_OK
Definition: virtio-ring.h:12
static u64 vpm_get_features(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:258
#define VIRTIO_CONFIG_S_FEATURES_OK
Definition: virtio-ring.h:14
uint8_t u8
Definition: stdint.h:19
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94

References DBGC, EINVAL, ENOENT, ENOMEM, features, INIT_LIST_HEAD, netdev, netdev_irq(), net_device::priv, QUEUE_NB, virtnet_nic::rx_iobufs, virtnet_nic::rx_num_iobufs, status, virtnet_nic::vdev, VIRTIO_CONFIG_S_DRIVER_OK, VIRTIO_CONFIG_S_FAILED, VIRTIO_CONFIG_S_FEATURES_OK, VIRTIO_F_ANY_LAYOUT, VIRTIO_F_IOMMU_PLATFORM, VIRTIO_F_VERSION_1, VIRTIO_NET_F_MAC, VIRTIO_NET_F_MTU, virtnet_free_virtqueues(), virtnet_refill_rx_virtqueue(), virtnet_nic::virtqueue, vpm_add_status(), vpm_find_vqs(), vpm_get_features(), vpm_get_status(), vpm_set_features(), and zalloc().

Referenced by virtnet_open().

◆ virtnet_open()

static int virtnet_open ( struct net_device netdev)
static

Open network device.

Parameters
netdevNetwork device
Return values
rcReturn status code

Definition at line 308 of file virtio-net.c.

308  {
309  struct virtnet_nic *virtnet = netdev->priv;
310 
311  if ( virtnet->virtio_version ) {
312  return virtnet_open_modern ( netdev );
313  } else {
314  return virtnet_open_legacy ( netdev );
315  }
316 }
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
static int virtnet_open_modern(struct net_device *netdev)
Open network device, modern virtio 1.0.
Definition: virtio-net.c:247
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:88
static int virtnet_open_legacy(struct net_device *netdev)
Open network device, legacy virtio 0.9.5.
Definition: virtio-net.c:201

References netdev, net_device::priv, virtnet_nic::virtio_version, virtnet_open_legacy(), and virtnet_open_modern().

◆ virtnet_close()

static void virtnet_close ( struct net_device netdev)
static

Close network device.

Parameters
netdevNetwork device

Definition at line 322 of file virtio-net.c.

322  {
323  struct virtnet_nic *virtnet = netdev->priv;
324  struct io_buffer *iobuf;
325  struct io_buffer *next_iobuf;
326 
327  if ( virtnet->virtio_version ) {
328  vpm_reset ( &virtnet->vdev );
329  } else {
330  vp_reset ( virtnet->ioaddr );
331  }
332 
333  /* Virtqueues can be freed now that NIC is reset */
335 
336  /* Free rx iobufs */
337  list_for_each_entry_safe ( iobuf, next_iobuf, &virtnet->rx_iobufs, list ) {
338  free_iob ( iobuf );
339  }
340  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
341  virtnet->rx_num_iobufs = 0;
342 }
static void virtnet_free_virtqueues(struct net_device *netdev)
Helper to free all virtqueue memory.
Definition: virtio-net.c:183
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
static void vpm_reset(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:238
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:100
#define list_for_each_entry_safe(pos, tmp, head, member)
Iterate over entries in a list, safe against deletion of the current entry.
Definition: list.h:447
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:88
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:39
struct list_head rx_iobufs
RX packets handed to the NIC waiting to be filled in.
Definition: virtio-net.c:97
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:85
static void vp_reset(unsigned int ioaddr)
Definition: virtio-pci.h:175
A persistent I/O buffer.
Definition: iobuf.h:32

References free_iob(), INIT_LIST_HEAD, virtnet_nic::ioaddr, io_buffer::list, list_for_each_entry_safe, netdev, net_device::priv, virtnet_nic::rx_iobufs, virtnet_nic::rx_num_iobufs, virtnet_nic::vdev, virtnet_nic::virtio_version, virtnet_free_virtqueues(), vp_reset(), and vpm_reset().

◆ virtnet_transmit()

static int virtnet_transmit ( struct net_device netdev,
struct io_buffer iobuf 
)
static

Transmit packet.

Parameters
netdevNetwork device
iobufI/O buffer
Return values
rcReturn status code

Definition at line 350 of file virtio-net.c.

351  {
352  virtnet_enqueue_iob ( netdev, TX_INDEX, iobuf );
353  return 0;
354 }
static void virtnet_enqueue_iob(struct net_device *netdev, int vq_idx, struct io_buffer *iobuf)
Add an iobuf to a virtqueue.
Definition: virtio-net.c:114
static struct net_device * netdev
Definition: gdbudp.c:52

References netdev, TX_INDEX, and virtnet_enqueue_iob().

◆ virtnet_process_tx_packets()

static void virtnet_process_tx_packets ( struct net_device netdev)
static

Complete packet transmission.

Parameters
netdevNetwork device

Definition at line 360 of file virtio-net.c.

360  {
361  struct virtnet_nic *virtnet = netdev->priv;
362  struct vring_virtqueue *tx_vq = &virtnet->virtqueue[TX_INDEX];
363 
364  while ( vring_more_used ( tx_vq ) ) {
365  struct io_buffer *iobuf = vring_get_buf ( tx_vq, NULL );
366 
367  DBGC2 ( virtnet, "VIRTIO-NET %p tx complete iobuf %p\n",
368  virtnet, iobuf );
369 
370  netdev_tx_complete ( netdev, iobuf );
371  }
372 }
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition: netdevice.h:746
static int vring_more_used(struct vring_virtqueue *vq)
Definition: virtio-ring.h:136
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
void * vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
Definition: virtio-ring.c:62
#define DBGC2(...)
Definition: compiler.h:522
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94
A persistent I/O buffer.
Definition: iobuf.h:32

References DBGC2, netdev, netdev_tx_complete(), NULL, net_device::priv, TX_INDEX, virtnet_nic::virtqueue, vring_get_buf(), and vring_more_used().

Referenced by virtnet_poll().

◆ virtnet_process_rx_packets()

static void virtnet_process_rx_packets ( struct net_device netdev)
static

Complete packet reception.

Parameters
netdevNetwork device

Definition at line 378 of file virtio-net.c.

378  {
379  struct virtnet_nic *virtnet = netdev->priv;
380  struct vring_virtqueue *rx_vq = &virtnet->virtqueue[RX_INDEX];
381 
382  while ( vring_more_used ( rx_vq ) ) {
383  unsigned int len;
384  struct io_buffer *iobuf = vring_get_buf ( rx_vq, &len );
385 
386  /* Release ownership of iobuf */
387  list_del ( &iobuf->list );
388  virtnet->rx_num_iobufs--;
389 
390  /* Update iobuf length */
391  iob_unput ( iobuf, iob_len ( iobuf ) );
392  iob_put ( iobuf, len - sizeof ( struct virtio_net_hdr ) );
393 
394  DBGC2 ( virtnet, "VIRTIO-NET %p rx complete iobuf %p len %zd\n",
395  virtnet, iobuf, iob_len ( iobuf ) );
396 
397  /* Pass completed packet to the network stack */
398  netdev_rx ( netdev, iobuf );
399  }
400 
402 }
#define iob_put(iobuf, len)
Definition: iobuf.h:116
static int vring_more_used(struct vring_virtqueue *vq)
Definition: virtio-ring.h:136
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:100
#define iob_unput(iobuf, len)
Definition: iobuf.h:131
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
void * vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
Definition: virtio-ring.c:62
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:470
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:39
uint32_t len
Length.
Definition: ena.h:14
#define DBGC2(...)
Definition: compiler.h:522
static void virtnet_refill_rx_virtqueue(struct net_device *netdev)
Try to keep rx virtqueue filled with iobufs.
Definition: virtio-net.c:156
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94
A persistent I/O buffer.
Definition: iobuf.h:32

References DBGC2, iob_len(), iob_put, iob_unput, len, io_buffer::list, list_del, netdev, netdev_rx(), net_device::priv, RX_INDEX, virtnet_nic::rx_num_iobufs, virtnet_refill_rx_virtqueue(), virtnet_nic::virtqueue, vring_get_buf(), and vring_more_used().

Referenced by virtnet_poll().

◆ virtnet_poll()

static void virtnet_poll ( struct net_device netdev)
static

Poll for completed and received packets.

Parameters
netdevNetwork device

Definition at line 408 of file virtio-net.c.

408  {
409  struct virtnet_nic *virtnet = netdev->priv;
410 
411  /* Acknowledge interrupt. This is necessary for UNDI operation and
412  * interrupts that are raised despite VRING_AVAIL_F_NO_INTERRUPT being
413  * set (that flag is just a hint and the hypervisor does not have to
414  * honor it).
415  */
416  if ( virtnet->virtio_version ) {
417  vpm_get_isr ( &virtnet->vdev );
418  } else {
419  vp_get_isr ( virtnet->ioaddr );
420  }
421 
424 }
static void virtnet_process_rx_packets(struct net_device *netdev)
Complete packet reception.
Definition: virtio-net.c:378
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
static u8 vp_get_isr(unsigned int ioaddr)
Definition: virtio-pci.h:170
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:88
static void virtnet_process_tx_packets(struct net_device *netdev)
Complete packet transmission.
Definition: virtio-net.c:360
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91
static u8 vpm_get_isr(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:292
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:85

References virtnet_nic::ioaddr, netdev, net_device::priv, virtnet_nic::vdev, virtnet_nic::virtio_version, virtnet_process_rx_packets(), virtnet_process_tx_packets(), vp_get_isr(), and vpm_get_isr().

◆ virtnet_irq()

static void virtnet_irq ( struct net_device netdev,
int  enable 
)
static

Enable or disable interrupts.

Parameters
netdevNetwork device
enableInterrupts should be enabled

Definition at line 431 of file virtio-net.c.

431  {
432  struct virtnet_nic *virtnet = netdev->priv;
433  int i;
434 
435  for ( i = 0; i < QUEUE_NB; i++ ) {
436  if ( enable )
437  vring_enable_cb ( &virtnet->virtqueue[i] );
438  else
439  vring_disable_cb ( &virtnet->virtqueue[i] );
440  }
441 }
static void vring_disable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:123
static void vring_enable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:118
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:94

References netdev, net_device::priv, QUEUE_NB, virtnet_nic::virtqueue, vring_disable_cb(), and vring_enable_cb().

◆ virtnet_probe_legacy()

static int virtnet_probe_legacy ( struct pci_device pci)
static

Probe PCI device, legacy virtio 0.9.5.

Parameters
pciPCI device
Return values
rcReturn status code

Definition at line 458 of file virtio-net.c.

458  {
459  unsigned long ioaddr = pci->ioaddr;
460  struct net_device *netdev;
461  struct virtnet_nic *virtnet;
462  u32 features;
463  u16 mtu;
464  int rc;
465 
466  /* Allocate and hook up net device */
467  netdev = alloc_etherdev ( sizeof ( *virtnet ) );
468  if ( ! netdev )
469  return -ENOMEM;
471  virtnet = netdev->priv;
472  virtnet->ioaddr = ioaddr;
473  pci_set_drvdata ( pci, netdev );
474  netdev->dev = &pci->dev;
475 
476  DBGC ( virtnet, "VIRTIO-NET %p busaddr=%s ioaddr=%#lx irq=%d\n",
477  virtnet, pci->dev.name, ioaddr, pci->irq );
478 
479  /* Enable PCI bus master and reset NIC */
480  adjust_pci_device ( pci );
481  vp_reset ( ioaddr );
482 
483  /* Load MAC address and MTU */
485  if ( features & ( 1 << VIRTIO_NET_F_MAC ) ) {
486  vp_get ( ioaddr, offsetof ( struct virtio_net_config, mac ),
487  netdev->hw_addr, ETH_ALEN );
488  DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
489  eth_ntoa ( netdev->hw_addr ) );
490  }
491  if ( features & ( 1ULL << VIRTIO_NET_F_MTU ) ) {
492  vp_get ( ioaddr, offsetof ( struct virtio_net_config, mtu ),
493  &mtu, sizeof ( mtu ) );
494  DBGC ( virtnet, "VIRTIO-NET %p mtu=%d\n", virtnet, mtu );
495  netdev->max_pkt_len = ( mtu + ETH_HLEN );
496  netdev->mtu = mtu;
497  }
498 
499  /* Register network device */
500  if ( ( rc = register_netdev ( netdev ) ) != 0 )
501  goto err_register_netdev;
502 
503  /* Mark link as up, control virtqueue is not used */
505 
506  return 0;
507 
509  err_register_netdev:
510  vp_reset ( ioaddr );
512  netdev_put ( netdev );
513  return rc;
514 }
uint16_t u16
Definition: stdint.h:21
uint8_t irq
Interrupt number.
Definition: pci.h:208
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
unsigned long ioaddr
I/O address.
Definition: pci.h:200
static u32 vp_get_features(unsigned int ioaddr)
Definition: virtio-pci.h:138
size_t mtu
Maximum transmission unit length.
Definition: netdevice.h:409
#define DBGC(...)
Definition: compiler.h:505
char name[40]
Name.
Definition: device.h:75
#define VIRTIO_NET_F_MTU
Definition: virtio-net.h:7
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
uint8_t mac[ETH_ALEN]
MAC address.
Definition: ena.h:24
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:149
struct device dev
Generic device.
Definition: pci.h:189
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:498
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:338
#define ENOMEM
Not enough space.
Definition: errno.h:534
static unsigned long ioaddr
Definition: davicom.c:129
#define ETH_HLEN
Definition: if_ether.h:9
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
void * priv
Driver private data.
Definition: netdevice.h:425
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:768
static struct net_device * netdev
Definition: gdbudp.c:52
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:667
uint32_t features
Supported features.
Definition: ena.h:16
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
A network device.
Definition: netdevice.h:348
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
#define ETH_ALEN
Definition: if_ether.h:8
#define VIRTIO_NET_F_MAC
Definition: virtio-net.h:8
struct device * dev
Underlying hardware device.
Definition: netdevice.h:360
static struct net_device_operations virtnet_operations
virtio-net device operations
Definition: virtio-net.c:444
uint32_t mtu
Maximum MTU.
Definition: ena.h:28
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
static void vp_get(unsigned int ioaddr, unsigned offset, void *buf, unsigned len)
Definition: virtio-pci.h:148
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:403
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:85
static void vp_reset(unsigned int ioaddr)
Definition: virtio-pci.h:175
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:375
uint32_t u32
Definition: stdint.h:23

References adjust_pci_device(), alloc_etherdev(), DBGC, pci_device::dev, net_device::dev, ENOMEM, ETH_ALEN, ETH_HLEN, eth_ntoa(), features, net_device::hw_addr, virtnet_nic::ioaddr, ioaddr, pci_device::ioaddr, pci_device::irq, mac, net_device::max_pkt_len, mtu, net_device::mtu, device::name, netdev, netdev_init(), netdev_link_up(), netdev_nullify(), netdev_put(), offsetof, pci_set_drvdata(), net_device::priv, rc, register_netdev(), unregister_netdev(), VIRTIO_NET_F_MAC, VIRTIO_NET_F_MTU, virtnet_operations, vp_get(), vp_get_features(), and vp_reset().

Referenced by virtnet_probe().

◆ virtnet_probe_modern()

static int virtnet_probe_modern ( struct pci_device pci,
int *  found_dev 
)
static

Probe PCI device, modern virtio 1.0.

Parameters
pciPCI device
found_devSet to non-zero if modern device was found (probe may still fail)
Return values
rcReturn status code

Definition at line 523 of file virtio-net.c.

523  {
524  struct net_device *netdev;
525  struct virtnet_nic *virtnet;
526  u64 features;
527  u16 mtu;
528  int rc, common, isr, notify, config, device;
529 
531  if ( ! common ) {
532  DBG ( "Common virtio capability not found!\n" );
533  return -ENODEV;
534  }
535  *found_dev = 1;
536 
540  if ( ! isr || ! notify || ! config ) {
541  DBG ( "Missing virtio capabilities %i/%i/%i/%i\n",
542  common, isr, notify, config );
543  return -EINVAL;
544  }
546 
547  /* Allocate and hook up net device */
548  netdev = alloc_etherdev ( sizeof ( *virtnet ) );
549  if ( ! netdev )
550  return -ENOMEM;
552  virtnet = netdev->priv;
553 
554  pci_set_drvdata ( pci, netdev );
555  netdev->dev = &pci->dev;
556 
557  DBGC ( virtnet, "VIRTIO-NET modern %p busaddr=%s irq=%d\n",
558  virtnet, pci->dev.name, pci->irq );
559 
560  virtnet->vdev.pci = pci;
562  sizeof ( struct virtio_pci_common_cfg ), 4,
563  0, sizeof ( struct virtio_pci_common_cfg ),
564  &virtnet->vdev.common );
565  if ( rc )
566  goto err_map_common;
567 
568  rc = virtio_pci_map_capability ( pci, isr, sizeof ( u8 ), 1,
569  0, 1,
570  &virtnet->vdev.isr );
571  if ( rc )
572  goto err_map_isr;
573 
574  virtnet->vdev.notify_cap_pos = notify;
575  virtnet->vdev.cfg_cap_pos = config;
576 
577  /* Map the device capability */
578  if ( device ) {
580  0, 4, 0, sizeof ( struct virtio_net_config ),
581  &virtnet->vdev.device );
582  if ( rc )
583  goto err_map_device;
584  }
585 
586  /* Enable the PCI device */
587  adjust_pci_device ( pci );
588 
589  /* Reset the device and set initial status bits */
590  vpm_reset ( &virtnet->vdev );
593 
594  /* Load MAC address and MTU */
595  if ( device ) {
596  features = vpm_get_features ( &virtnet->vdev );
597  if ( features & ( 1ULL << VIRTIO_NET_F_MAC ) ) {
598  vpm_get ( &virtnet->vdev,
599  offsetof ( struct virtio_net_config, mac ),
600  netdev->hw_addr, ETH_ALEN );
601  DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
602  eth_ntoa ( netdev->hw_addr ) );
603  }
604  if ( features & ( 1ULL << VIRTIO_NET_F_MTU ) ) {
605  vpm_get ( &virtnet->vdev,
606  offsetof ( struct virtio_net_config, mtu ),
607  &mtu, sizeof ( mtu ) );
608  DBGC ( virtnet, "VIRTIO-NET %p mtu=%d\n", virtnet,
609  mtu );
610  netdev->max_pkt_len = ( mtu + ETH_HLEN );
611  }
612  }
613 
614  /* We need a valid MAC address */
615  if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
616  rc = -EADDRNOTAVAIL;
617  goto err_mac_address;
618  }
619 
620  /* Register network device */
621  if ( ( rc = register_netdev ( netdev ) ) != 0 )
622  goto err_register_netdev;
623 
624  /* Mark link as up, control virtqueue is not used */
626 
627  virtnet->virtio_version = 1;
628  return 0;
629 
631 err_register_netdev:
632 err_mac_address:
633  vpm_reset ( &virtnet->vdev );
635  netdev_put ( netdev );
636 
638 err_map_device:
639  virtio_pci_unmap_capability ( &virtnet->vdev.isr );
640 err_map_isr:
642 err_map_common:
643  return rc;
644 }
uint16_t u16
Definition: stdint.h:21
#define EINVAL
Invalid argument.
Definition: errno.h:428
uint8_t irq
Interrupt number.
Definition: pci.h:208
static void vpm_get(struct virtio_pci_modern_device *vdev, unsigned offset, void *buf, unsigned len)
Definition: virtio-pci.h:282
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct pci_device * pci
Definition: virtio-pci.h:120
#define DBGC(...)
Definition: compiler.h:505
#define VIRTIO_PCI_CAP_PCI_CFG
Definition: virtio-pci.h:45
char name[40]
Name.
Definition: device.h:75
#define VIRTIO_NET_F_MTU
Definition: virtio-net.h:7
#define VIRTIO_PCI_CAP_DEVICE_CFG
Definition: virtio-pci.h:44
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
uint8_t mac[ETH_ALEN]
MAC address.
Definition: ena.h:24
#define VIRTIO_PCI_CAP_COMMON_CFG
Definition: virtio-pci.h:41
uint16_t device
Device ID.
Definition: ena.h:24
int virtio_pci_map_capability(struct pci_device *pci, int cap, size_t minlen, u32 align, u32 start, u32 size, struct virtio_pci_region *region)
Definition: virtio-pci.c:266
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:149
struct device dev
Generic device.
Definition: pci.h:189
static void vpm_reset(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:238
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:498
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:338
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:73
struct virtio_pci_region device
Definition: virtio-pci.h:129
#define ETH_HLEN
Definition: if_ether.h:9
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
void * priv
Driver private data.
Definition: netdevice.h:425
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:768
int virtio_pci_find_capability(struct pci_device *pci, uint8_t cfg_type)
Definition: virtio-pci.c:240
static struct net_device * netdev
Definition: gdbudp.c:52
uint64_t u64
Definition: stdint.h:25
Definition: sis900.h:26
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
void virtio_pci_unmap_capability(struct virtio_pci_region *region)
Definition: virtio-pci.c:336
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:667
uint32_t features
Supported features.
Definition: ena.h:16
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
struct virtio_pci_region common
Definition: virtio-pci.h:126
struct virtio_pci_region isr
Definition: virtio-pci.h:132
A network device.
Definition: netdevice.h:348
#define ENODEV
No such device.
Definition: errno.h:509
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
#define VIRTIO_CONFIG_S_ACKNOWLEDGE
Definition: virtio-ring.h:8
#define VIRTIO_PCI_CAP_NOTIFY_CFG
Definition: virtio-pci.h:42
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:88
#define ETH_ALEN
Definition: if_ether.h:8
#define VIRTIO_NET_F_MAC
Definition: virtio-net.h:8
struct ib_cm_common common
Definition: ib_mad.h:11
static int is_valid_ether_addr(const void *addr)
Check if Ethernet address is valid.
Definition: ethernet.h:77
struct device * dev
Underlying hardware device.
Definition: netdevice.h:360
#define EADDRNOTAVAIL
Address not available.
Definition: errno.h:308
#define VIRTIO_PCI_CAP_ISR_CFG
Definition: virtio-pci.h:43
#define VIRTIO_CONFIG_S_DRIVER
Definition: virtio-ring.h:10
static struct net_device_operations virtnet_operations
virtio-net device operations
Definition: virtio-net.c:444
static void vpm_add_status(struct virtio_pci_modern_device *vdev, u8 status)
Definition: virtio-pci.h:250
uint32_t mtu
Maximum MTU.
Definition: ena.h:28
struct net_device * alloc_etherdev(size_t priv_size)
Allocate Ethernet device.
Definition: ethernet.c:264
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:403
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static u64 vpm_get_features(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:258
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:375
uint8_t u8
Definition: stdint.h:19

References adjust_pci_device(), alloc_etherdev(), virtio_pci_modern_device::cfg_cap_pos, virtio_pci_modern_device::common, common, DBG, DBGC, pci_device::dev, net_device::dev, device, virtio_pci_modern_device::device, EADDRNOTAVAIL, EINVAL, ENODEV, ENOMEM, ETH_ALEN, ETH_HLEN, eth_ntoa(), features, net_device::hw_addr, pci_device::irq, is_valid_ether_addr(), isr, virtio_pci_modern_device::isr, mac, net_device::max_pkt_len, mtu, device::name, netdev, netdev_init(), netdev_link_up(), netdev_nullify(), netdev_put(), virtio_pci_modern_device::notify_cap_pos, offsetof, virtio_pci_modern_device::pci, pci_set_drvdata(), net_device::priv, rc, register_netdev(), unregister_netdev(), virtnet_nic::vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE, VIRTIO_CONFIG_S_DRIVER, VIRTIO_NET_F_MAC, VIRTIO_NET_F_MTU, VIRTIO_PCI_CAP_COMMON_CFG, VIRTIO_PCI_CAP_DEVICE_CFG, VIRTIO_PCI_CAP_ISR_CFG, VIRTIO_PCI_CAP_NOTIFY_CFG, VIRTIO_PCI_CAP_PCI_CFG, virtio_pci_find_capability(), virtio_pci_map_capability(), virtio_pci_unmap_capability(), virtnet_nic::virtio_version, virtnet_operations, vpm_add_status(), vpm_get(), vpm_get_features(), and vpm_reset().

Referenced by virtnet_probe().

◆ virtnet_probe()

static int virtnet_probe ( struct pci_device pci)
static

Probe PCI device.

Parameters
pciPCI device
Return values
rcReturn status code

Definition at line 652 of file virtio-net.c.

652  {
653  int found_modern = 0;
654  int rc = virtnet_probe_modern ( pci, &found_modern );
655  if ( ! found_modern && pci->device < 0x1040 ) {
656  /* fall back to the legacy probe */
657  rc = virtnet_probe_legacy ( pci );
658  }
659  return rc;
660 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static int virtnet_probe_modern(struct pci_device *pci, int *found_dev)
Probe PCI device, modern virtio 1.0.
Definition: virtio-net.c:523
uint16_t device
Device ID.
Definition: pci.h:204
static int virtnet_probe_legacy(struct pci_device *pci)
Probe PCI device, legacy virtio 0.9.5.
Definition: virtio-net.c:458

References pci_device::device, rc, virtnet_probe_legacy(), and virtnet_probe_modern().

◆ virtnet_remove()

static void virtnet_remove ( struct pci_device pci)
static

Remove device.

Parameters
pciPCI device

Definition at line 667 of file virtio-net.c.

667  {
668  struct net_device *netdev = pci_get_drvdata ( pci );
669  struct virtnet_nic *virtnet = netdev->priv;
670 
672  virtio_pci_unmap_capability ( &virtnet->vdev.isr );
674 
677  netdev_put ( netdev );
678 }
struct virtio_pci_region device
Definition: virtio-pci.h:129
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:555
void * priv
Driver private data.
Definition: netdevice.h:425
static struct net_device * netdev
Definition: gdbudp.c:52
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:844
void virtio_pci_unmap_capability(struct virtio_pci_region *region)
Definition: virtio-pci.c:336
struct virtio_pci_region common
Definition: virtio-pci.h:126
struct virtio_pci_region isr
Definition: virtio-pci.h:132
A network device.
Definition: netdevice.h:348
static void netdev_nullify(struct net_device *netdev)
Stop using a network device.
Definition: netdevice.h:511
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:348
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:91

References virtio_pci_modern_device::common, virtio_pci_modern_device::device, virtio_pci_modern_device::isr, netdev, netdev_nullify(), netdev_put(), pci_get_drvdata(), net_device::priv, unregister_netdev(), virtnet_nic::vdev, and virtio_pci_unmap_capability().

Variable Documentation

◆ virtnet_operations

struct net_device_operations virtnet_operations
static
Initial value:
= {
.open = virtnet_open,
.close = virtnet_close,
.transmit = virtnet_transmit,
.poll = virtnet_poll,
.irq = virtnet_irq,
}
static void virtnet_close(struct net_device *netdev)
Close network device.
Definition: virtio-net.c:322
static void virtnet_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition: virtio-net.c:408
static void virtnet_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: virtio-net.c:431
static int virtnet_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: virtio-net.c:350
static int virtnet_open(struct net_device *netdev)
Open network device.
Definition: virtio-net.c:308

virtio-net device operations

Definition at line 444 of file virtio-net.c.

Referenced by virtnet_probe_legacy(), and virtnet_probe_modern().

◆ virtnet_nics

struct pci_device_id virtnet_nics[]
static
Initial value:
= {
PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface", 0),
PCI_ROM(0x1af4, 0x1041, "virtio-net", "Virtio Network Interface 1.0", 0),
}
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:283

Definition at line 680 of file virtio-net.c.

◆ __pci_driver

struct pci_driver virtnet_driver __pci_driver
Initial value:
= {
.ids = virtnet_nics,
.id_count = ( sizeof ( virtnet_nics ) / sizeof ( virtnet_nics[0] ) ),
.probe = virtnet_probe,
}
static void virtnet_remove(struct pci_device *pci)
Remove device.
Definition: virtio-net.c:667
static struct pci_device_id virtnet_nics[]
Definition: virtio-net.c:680
static struct xen_remove_from_physmap * remove
Definition: xenmem.h:39
static int virtnet_probe(struct pci_device *pci)
Probe PCI device.
Definition: virtio-net.c:652

Definition at line 685 of file virtio-net.c.