iPXE
virtio-net.c
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2010 Stefan Hajnoczi <stefanha@gmail.com>
3  *
4  * based on the Etherboot virtio-net driver
5  *
6  * (c) Copyright 2008 Bull S.A.S.
7  *
8  * Author: Laurent Vivier <Laurent.Vivier@bull.net>
9  *
10  * some parts from Linux Virtio PCI driver
11  *
12  * Copyright IBM Corp. 2007
13  * Authors: Anthony Liguori <aliguori@us.ibm.com>
14  *
15  * some parts from Linux Virtio Ring
16  *
17  * Copyright Rusty Russell IBM Corporation 2007
18  *
19  * This work is licensed under the terms of the GNU GPL, version 2 or later.
20  * See the COPYING file in the top-level directory.
21  */
22 
23 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
24 
25 #include <errno.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <ipxe/list.h>
29 #include <ipxe/iobuf.h>
30 #include <ipxe/netdevice.h>
31 #include <ipxe/pci.h>
32 #include <ipxe/dma.h>
33 #include <ipxe/if_ether.h>
34 #include <ipxe/ethernet.h>
35 #include <ipxe/virtio-pci.h>
36 #include <ipxe/virtio-ring.h>
37 #include "virtio-net.h"
38 
39 /*
40  * Virtio network device driver
41  *
42  * Specification:
43  * http://ozlabs.org/~rusty/virtio-spec/
44  *
45  * The virtio network device is supported by Linux virtualization software
46  * including QEMU/KVM and lguest. This driver supports the virtio over PCI
47  * transport; virtual machines have one virtio-net PCI adapter per NIC.
48  *
49  * Virtio-net is different from hardware NICs because virtio devices
50  * communicate with the hypervisor via virtqueues, not traditional descriptor
51  * rings. Virtqueues are unordered queues, they support add_buf() and
52  * get_buf() operations. To transmit a packet, the driver has to add the
53  * packet buffer onto the virtqueue. To receive a packet, the driver must
54  * first add an empty buffer to the virtqueue and then get the filled packet
55  * buffer on completion.
56  *
57  * Virtqueues are an abstraction that is commonly implemented using the vring
58  * descriptor ring layout. The vring is the actual shared memory structure
59  * that allows the virtual machine to communicate buffers with the hypervisor.
60  * Because the vring layout is optimized for flexibility and performance rather
61  * than space, it is heavy-weight and allocated like traditional descriptor
62  * rings in the open() function of the driver and not in probe().
63  *
64  * There is no true interrupt enable/disable. Virtqueues have callback
65  * enable/disable flags but these are only hints. The hypervisor may still
66  * raise an interrupt. Nevertheless, this driver disables callbacks in the
67  * hopes of avoiding interrupts.
68  */
69 
70 /* Driver types are declared here so virtio-net.h can be easily synced with its
71  * Linux source.
72  */
73 
74 /* Virtqueue indices */
75 enum {
76  RX_INDEX = 0,
79 };
80 
81 /** Max number of pending rx packets */
82 #define NUM_RX_BUF 8
83 
84 struct virtnet_nic {
85  /** Base pio register address */
86  unsigned long ioaddr;
87 
88  /** 0 for legacy, 1 for virtio 1.0 */
90 
91  /** Virtio 1.0 device data */
93 
94  /** RX/TX virtqueues */
96 
97  /** RX packets handed to the NIC waiting to be filled in */
99 
100  /** Pending rx packet count */
101  unsigned int rx_num_iobufs;
102 
103  /** DMA device */
104  struct dma_device *dma;
105 
106 };
107 
108 /** Add an iobuf to a virtqueue
109  *
110  * @v netdev Network device
111  * @v vq_idx Virtqueue index (RX_INDEX or TX_INDEX)
112  * @v iobuf I/O buffer
113  *
114  * The virtqueue is kicked after the iobuf has been added.
115  */
116 static void virtnet_enqueue_iob ( struct net_device *netdev,
117  int vq_idx, struct io_buffer *iobuf ) {
118  struct virtnet_nic *virtnet = netdev->priv;
119  struct vring_virtqueue *vq = &virtnet->virtqueue[vq_idx];
121  unsigned int out = ( vq_idx == TX_INDEX ) ? 2 : 0;
122  unsigned int in = ( vq_idx == TX_INDEX ) ? 0 : 2;
123  size_t header_len = ( virtnet->virtio_version ?
124  sizeof ( *header ) : sizeof ( header->legacy ) );
125  struct vring_list list[] = {
126  {
127  /* Share a single zeroed virtio net header between all
128  * packets in a ring. This works because this driver
129  * does not use any advanced features so none of the
130  * header fields get used.
131  *
132  * Some host implementations (notably Google Compute
133  * Platform) are known to unconditionally write back
134  * to header->flags for received packets. Work around
135  * this by using separate RX and TX headers.
136  */
137  .addr = dma ( &vq->map, header ),
138  .length = header_len,
139  },
140  {
141  .addr = iob_dma ( iobuf ),
142  .length = iob_len ( iobuf ),
143  },
144  };
145 
146  DBGC2 ( virtnet, "VIRTIO-NET %p enqueuing iobuf %p on vq %d\n",
147  virtnet, iobuf, vq_idx );
148 
149  vring_add_buf ( vq, list, out, in, iobuf, 0 );
150  vring_kick ( virtnet->virtio_version ? &virtnet->vdev : NULL,
151  virtnet->ioaddr, vq, 1 );
152 }
153 
154 /** Try to keep rx virtqueue filled with iobufs
155  *
156  * @v netdev Network device
157  */
159  struct virtnet_nic *virtnet = netdev->priv;
160  size_t len = ( netdev->max_pkt_len + 4 /* VLAN */ );
161 
162  while ( virtnet->rx_num_iobufs < NUM_RX_BUF ) {
163  struct io_buffer *iobuf;
164 
165  /* Try to allocate a buffer, stop for now if out of memory */
166  iobuf = alloc_rx_iob ( len, virtnet->dma );
167  if ( ! iobuf )
168  break;
169 
170  /* Keep track of iobuf so close() can free it */
171  list_add ( &iobuf->list, &virtnet->rx_iobufs );
172 
173  /* Mark packet length until we know the actual size */
174  iob_put ( iobuf, len );
175 
176  virtnet_enqueue_iob ( netdev, RX_INDEX, iobuf );
177  virtnet->rx_num_iobufs++;
178  }
179 }
180 
181 /** Helper to free all virtqueue memory
182  *
183  * @v netdev Network device
184  */
185 static void virtnet_free_virtqueues ( struct net_device *netdev ) {
186  struct virtnet_nic *virtnet = netdev->priv;
187  int i;
188 
189  for ( i = 0; i < QUEUE_NB; i++ ) {
191  vp_free_vq ( &virtnet->virtqueue[i] );
192  }
193 
194  free ( virtnet->virtqueue );
195  virtnet->virtqueue = NULL;
196 }
197 
198 /** Open network device, legacy virtio 0.9.5
199  *
200  * @v netdev Network device
201  * @ret rc Return status code
202  */
203 static int virtnet_open_legacy ( struct net_device *netdev ) {
204  struct virtnet_nic *virtnet = netdev->priv;
205  unsigned long ioaddr = virtnet->ioaddr;
206  u32 features;
207  int i;
208 
209  /* Reset for sanity */
210  vp_reset ( ioaddr );
211 
212  /* Allocate virtqueues */
213  virtnet->virtqueue = zalloc ( QUEUE_NB *
214  sizeof ( *virtnet->virtqueue ) );
215  if ( ! virtnet->virtqueue )
216  return -ENOMEM;
217 
218  /* Initialize rx/tx virtqueues */
219  for ( i = 0; i < QUEUE_NB; i++ ) {
220  if ( vp_find_vq ( ioaddr, i, &virtnet->virtqueue[i], virtnet->dma,
221  sizeof ( struct virtio_net_hdr_modern ) ) == -1 ) {
222  DBGC ( virtnet, "VIRTIO-NET %p cannot register queue %d\n",
223  virtnet, i );
225  return -ENOENT;
226  }
227  }
228 
229  /* Initialize rx packets */
230  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
231  virtnet->rx_num_iobufs = 0;
233 
234  /* Disable interrupts before starting */
235  netdev_irq ( netdev, 0 );
236 
237  /* Driver is ready */
240  ( 1 << VIRTIO_NET_F_MTU ) ) );
242  return 0;
243 }
244 
245 /** Open network device, modern virtio 1.0
246  *
247  * @v netdev Network device
248  * @ret rc Return status code
249  */
250 static int virtnet_open_modern ( struct net_device *netdev ) {
251  struct virtnet_nic *virtnet = netdev->priv;
252  u64 features;
253  u8 status;
254 
255  /* Negotiate features */
256  features = vpm_get_features ( &virtnet->vdev );
257  if ( ! ( features & VIRTIO_F_VERSION_1 ) ) {
259  return -EINVAL;
260  }
261  vpm_set_features ( &virtnet->vdev, features & (
262  ( 1ULL << VIRTIO_NET_F_MAC ) |
263  ( 1ULL << VIRTIO_NET_F_MTU ) |
264  ( 1ULL << VIRTIO_F_VERSION_1 ) |
265  ( 1ULL << VIRTIO_F_ANY_LAYOUT ) |
266  ( 1ULL << VIRTIO_F_IOMMU_PLATFORM ) ) );
268 
269  status = vpm_get_status ( &virtnet->vdev );
270  if ( ! ( status & VIRTIO_CONFIG_S_FEATURES_OK ) ) {
271  DBGC ( virtnet, "VIRTIO-NET %p device didn't accept features\n",
272  virtnet );
274  return -EINVAL;
275  }
276 
277  /* Allocate virtqueues */
278  virtnet->virtqueue = zalloc ( QUEUE_NB *
279  sizeof ( *virtnet->virtqueue ) );
280  if ( ! virtnet->virtqueue ) {
282  return -ENOMEM;
283  }
284 
285  /* Initialize rx/tx virtqueues */
286  if ( vpm_find_vqs ( &virtnet->vdev, QUEUE_NB, virtnet->virtqueue,
287  virtnet->dma, sizeof ( struct virtio_net_hdr_modern ) ) ) {
288  DBGC ( virtnet, "VIRTIO-NET %p cannot register queues\n",
289  virtnet );
292  return -ENOENT;
293  }
294 
295  /* Disable interrupts before starting */
296  netdev_irq ( netdev, 0 );
297 
299 
300  /* Initialize rx packets */
301  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
302  virtnet->rx_num_iobufs = 0;
304  return 0;
305 }
306 
307 /** Open network device
308  *
309  * @v netdev Network device
310  * @ret rc Return status code
311  */
312 static int virtnet_open ( struct net_device *netdev ) {
313  struct virtnet_nic *virtnet = netdev->priv;
314 
315  if ( virtnet->virtio_version ) {
316  return virtnet_open_modern ( netdev );
317  } else {
318  return virtnet_open_legacy ( netdev );
319  }
320 }
321 
322 /** Close network device
323  *
324  * @v netdev Network device
325  */
326 static void virtnet_close ( struct net_device *netdev ) {
327  struct virtnet_nic *virtnet = netdev->priv;
328  struct io_buffer *iobuf;
329  struct io_buffer *next_iobuf;
330 
331  if ( virtnet->virtio_version ) {
332  vpm_reset ( &virtnet->vdev );
333  } else {
334  vp_reset ( virtnet->ioaddr );
335  }
336 
337  /* Virtqueues can be freed now that NIC is reset */
339 
340  /* Free rx iobufs */
341  list_for_each_entry_safe ( iobuf, next_iobuf, &virtnet->rx_iobufs, list ) {
342  free_rx_iob ( iobuf );
343  }
344  INIT_LIST_HEAD ( &virtnet->rx_iobufs );
345  virtnet->rx_num_iobufs = 0;
346 }
347 
348 /** Transmit packet
349  *
350  * @v netdev Network device
351  * @v iobuf I/O buffer
352  * @ret rc Return status code
353  */
354 static int virtnet_transmit ( struct net_device *netdev,
355  struct io_buffer *iobuf ) {
356  virtnet_enqueue_iob ( netdev, TX_INDEX, iobuf );
357  return 0;
358 }
359 
360 /** Complete packet transmission
361  *
362  * @v netdev Network device
363  */
365  struct virtnet_nic *virtnet = netdev->priv;
366  struct vring_virtqueue *tx_vq = &virtnet->virtqueue[TX_INDEX];
367 
368  while ( vring_more_used ( tx_vq ) ) {
369  struct io_buffer *iobuf = vring_get_buf ( tx_vq, NULL );
370 
371  DBGC2 ( virtnet, "VIRTIO-NET %p tx complete iobuf %p\n",
372  virtnet, iobuf );
373 
374  netdev_tx_complete ( netdev, iobuf );
375  }
376 }
377 
378 /** Complete packet reception
379  *
380  * @v netdev Network device
381  */
383  struct virtnet_nic *virtnet = netdev->priv;
384  struct vring_virtqueue *rx_vq = &virtnet->virtqueue[RX_INDEX];
385 
386  while ( vring_more_used ( rx_vq ) ) {
387  unsigned int len;
388  struct io_buffer *iobuf = vring_get_buf ( rx_vq, &len );
389 
390  /* Release ownership of iobuf */
391  list_del ( &iobuf->list );
392  virtnet->rx_num_iobufs--;
393 
394  /* Update iobuf length */
395  iob_unput ( iobuf, iob_len ( iobuf ) );
396  iob_put ( iobuf, len - sizeof ( struct virtio_net_hdr ) );
397 
398  DBGC2 ( virtnet, "VIRTIO-NET %p rx complete iobuf %p len %zd\n",
399  virtnet, iobuf, iob_len ( iobuf ) );
400 
401  /* Pass completed packet to the network stack */
402  netdev_rx ( netdev, iobuf );
403  }
404 
406 }
407 
408 /** Poll for completed and received packets
409  *
410  * @v netdev Network device
411  */
412 static void virtnet_poll ( struct net_device *netdev ) {
413  struct virtnet_nic *virtnet = netdev->priv;
414 
415  /* Acknowledge interrupt. This is necessary for UNDI operation and
416  * interrupts that are raised despite VRING_AVAIL_F_NO_INTERRUPT being
417  * set (that flag is just a hint and the hypervisor does not have to
418  * honor it).
419  */
420  if ( virtnet->virtio_version ) {
421  vpm_get_isr ( &virtnet->vdev );
422  } else {
423  vp_get_isr ( virtnet->ioaddr );
424  }
425 
428 }
429 
430 /** Enable or disable interrupts
431  *
432  * @v netdev Network device
433  * @v enable Interrupts should be enabled
434  */
435 static void virtnet_irq ( struct net_device *netdev, int enable ) {
436  struct virtnet_nic *virtnet = netdev->priv;
437  int i;
438 
439  for ( i = 0; i < QUEUE_NB; i++ ) {
440  if ( enable )
441  vring_enable_cb ( &virtnet->virtqueue[i] );
442  else
443  vring_disable_cb ( &virtnet->virtqueue[i] );
444  }
445 }
446 
447 /** virtio-net device operations */
449  .open = virtnet_open,
450  .close = virtnet_close,
451  .transmit = virtnet_transmit,
452  .poll = virtnet_poll,
453  .irq = virtnet_irq,
454 };
455 
456 /**
457  * Probe PCI device, legacy virtio 0.9.5
458  *
459  * @v pci PCI device
460  * @ret rc Return status code
461  */
462 static int virtnet_probe_legacy ( struct pci_device *pci ) {
463  unsigned long ioaddr = pci->ioaddr;
464  struct net_device *netdev;
465  struct virtnet_nic *virtnet;
466  u32 features;
467  u16 mtu;
468  int rc;
469 
470  /* Allocate and hook up net device */
471  netdev = alloc_etherdev ( sizeof ( *virtnet ) );
472  if ( ! netdev )
473  return -ENOMEM;
475  virtnet = netdev->priv;
476  virtnet->ioaddr = ioaddr;
477  pci_set_drvdata ( pci, netdev );
478  netdev->dev = &pci->dev;
479 
480  DBGC ( virtnet, "VIRTIO-NET %p busaddr=%s ioaddr=%#lx irq=%d\n",
481  virtnet, pci->dev.name, ioaddr, pci->irq );
482 
483  /* Enable PCI bus master and reset NIC */
484  adjust_pci_device ( pci );
485 
486  /* Configure DMA */
487  virtnet->dma = &pci->dma;
488  dma_set_mask_64bit ( virtnet->dma );
489  netdev->dma = virtnet->dma;
490 
491  vp_reset ( ioaddr );
492 
493  /* Load MAC address and MTU */
495  if ( features & ( 1 << VIRTIO_NET_F_MAC ) ) {
496  vp_get ( ioaddr, offsetof ( struct virtio_net_config, mac ),
497  netdev->hw_addr, ETH_ALEN );
498  DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
499  eth_ntoa ( netdev->hw_addr ) );
500  }
501  if ( features & ( 1ULL << VIRTIO_NET_F_MTU ) ) {
502  vp_get ( ioaddr, offsetof ( struct virtio_net_config, mtu ),
503  &mtu, sizeof ( mtu ) );
504  DBGC ( virtnet, "VIRTIO-NET %p mtu=%d\n", virtnet, mtu );
505  netdev->max_pkt_len = ( mtu + ETH_HLEN );
506  netdev->mtu = mtu;
507  }
508 
509  /* Register network device */
510  if ( ( rc = register_netdev ( netdev ) ) != 0 )
511  goto err_register_netdev;
512 
513  /* Mark link as up, control virtqueue is not used */
515 
516  return 0;
517 
519 err_register_netdev:
520  vp_reset ( ioaddr );
522  netdev_put ( netdev );
523  return rc;
524 }
525 
526 /**
527  * Probe PCI device, modern virtio 1.0
528  *
529  * @v pci PCI device
530  * @v found_dev Set to non-zero if modern device was found (probe may still fail)
531  * @ret rc Return status code
532  */
533 static int virtnet_probe_modern ( struct pci_device *pci, int *found_dev ) {
534  struct net_device *netdev;
535  struct virtnet_nic *virtnet;
536  u64 features;
537  u16 mtu;
538  int rc, common, isr, notify, config, device;
539 
541  if ( ! common ) {
542  DBG ( "Common virtio capability not found!\n" );
543  return -ENODEV;
544  }
545  *found_dev = 1;
546 
550  if ( ! isr || ! notify || ! config ) {
551  DBG ( "Missing virtio capabilities %i/%i/%i/%i\n",
552  common, isr, notify, config );
553  return -EINVAL;
554  }
556 
557  /* Allocate and hook up net device */
558  netdev = alloc_etherdev ( sizeof ( *virtnet ) );
559  if ( ! netdev )
560  return -ENOMEM;
562  virtnet = netdev->priv;
563 
564  pci_set_drvdata ( pci, netdev );
565  netdev->dev = &pci->dev;
566 
567  DBGC ( virtnet, "VIRTIO-NET modern %p busaddr=%s irq=%d\n",
568  virtnet, pci->dev.name, pci->irq );
569 
570  virtnet->vdev.pci = pci;
572  sizeof ( struct virtio_pci_common_cfg ), 4,
573  0, sizeof ( struct virtio_pci_common_cfg ),
574  &virtnet->vdev.common );
575  if ( rc )
576  goto err_map_common;
577 
578  rc = virtio_pci_map_capability ( pci, isr, sizeof ( u8 ), 1,
579  0, 1,
580  &virtnet->vdev.isr );
581  if ( rc )
582  goto err_map_isr;
583 
584  virtnet->vdev.notify_cap_pos = notify;
585  virtnet->vdev.cfg_cap_pos = config;
586 
587  /* Map the device capability */
588  if ( device ) {
590  0, 4, 0, sizeof ( struct virtio_net_config ),
591  &virtnet->vdev.device );
592  if ( rc )
593  goto err_map_device;
594  }
595 
596  /* Enable the PCI device */
597  adjust_pci_device ( pci );
598 
599  /* Configure DMA */
600  virtnet->dma = &pci->dma;
601  dma_set_mask_64bit ( virtnet->dma );
602  netdev->dma = virtnet->dma;
603 
604  /* Reset the device and set initial status bits */
605  vpm_reset ( &virtnet->vdev );
608 
609  /* Load MAC address and MTU */
610  if ( device ) {
611  features = vpm_get_features ( &virtnet->vdev );
612  if ( features & ( 1ULL << VIRTIO_NET_F_MAC ) ) {
613  vpm_get ( &virtnet->vdev,
614  offsetof ( struct virtio_net_config, mac ),
615  netdev->hw_addr, ETH_ALEN );
616  DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
617  eth_ntoa ( netdev->hw_addr ) );
618  }
619  if ( features & ( 1ULL << VIRTIO_NET_F_MTU ) ) {
620  vpm_get ( &virtnet->vdev,
621  offsetof ( struct virtio_net_config, mtu ),
622  &mtu, sizeof ( mtu ) );
623  DBGC ( virtnet, "VIRTIO-NET %p mtu=%d\n", virtnet,
624  mtu );
625  netdev->max_pkt_len = ( mtu + ETH_HLEN );
626  }
627  }
628 
629  /* We need a valid MAC address */
630  if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
631  rc = -EADDRNOTAVAIL;
632  goto err_mac_address;
633  }
634 
635  /* Register network device */
636  if ( ( rc = register_netdev ( netdev ) ) != 0 )
637  goto err_register_netdev;
638 
639  /* Mark link as up, control virtqueue is not used */
641 
642  virtnet->virtio_version = 1;
643  return 0;
644 
646 err_register_netdev:
647 err_mac_address:
648  vpm_reset ( &virtnet->vdev );
650  netdev_put ( netdev );
652 err_map_device:
653  virtio_pci_unmap_capability ( &virtnet->vdev.isr );
654 err_map_isr:
656 err_map_common:
657  return rc;
658 }
659 
660 /**
661  * Probe PCI device
662  *
663  * @v pci PCI device
664  * @ret rc Return status code
665  */
666 static int virtnet_probe ( struct pci_device *pci ) {
667  int found_modern = 0;
668  int rc = virtnet_probe_modern ( pci, &found_modern );
669  if ( ! found_modern && pci->device < 0x1040 ) {
670  /* fall back to the legacy probe */
671  rc = virtnet_probe_legacy ( pci );
672  }
673  return rc;
674 }
675 
676 /**
677  * Remove device
678  *
679  * @v pci PCI device
680  */
681 static void virtnet_remove ( struct pci_device *pci ) {
682  struct net_device *netdev = pci_get_drvdata ( pci );
683  struct virtnet_nic *virtnet = netdev->priv;
684 
686  virtio_pci_unmap_capability ( &virtnet->vdev.isr );
688 
691  netdev_put ( netdev );
692 }
693 
694 static struct pci_device_id virtnet_nics[] = {
695 PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface", 0),
696 PCI_ROM(0x1af4, 0x1041, "virtio-net", "Virtio Network Interface 1.0", 0),
697 };
698 
699 struct pci_driver virtnet_driver __pci_driver = {
700  .ids = virtnet_nics,
701  .id_count = ( sizeof ( virtnet_nics ) / sizeof ( virtnet_nics[0] ) ),
702  .probe = virtnet_probe,
704 };
uint16_t u16
Definition: stdint.h:21
void vring_kick(struct virtio_pci_modern_device *vdev, unsigned int ioaddr, struct vring_virtqueue *vq, int num_added)
Definition: virtio-ring.c:125
#define VIRTIO_F_VERSION_1
Definition: virtio-ring.h:23
static u8 vpm_get_status(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:248
static void vpm_set_features(struct virtio_pci_modern_device *vdev, u64 features)
Definition: virtio-pci.h:273
#define EINVAL
Invalid argument.
Definition: errno.h:428
uint8_t irq
Interrupt number.
Definition: pci.h:229
static void vpm_get(struct virtio_pci_modern_device *vdev, unsigned offset, void *buf, unsigned len)
Definition: virtio-pci.h:285
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
struct pci_device * pci
Definition: virtio-pci.h:122
static void netdev_tx_complete(struct net_device *netdev, struct io_buffer *iobuf)
Complete network transmission.
Definition: netdevice.h:752
DMA mappings.
#define iob_put(iobuf, len)
Definition: iobuf.h:120
struct dma_device dma
DMA device.
Definition: pci.h:210
static int vring_more_used(struct vring_virtqueue *vq)
Definition: virtio-ring.h:141
static void virtnet_remove(struct pci_device *pci)
Remove device.
Definition: virtio-net.c:681
A PCI driver.
Definition: pci.h:247
static void virtnet_free_virtqueues(struct net_device *netdev)
Helper to free all virtqueue memory.
Definition: virtio-net.c:185
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
__be32 in[4]
Definition: CIB_PRM.h:35
int(* open)(struct net_device *netdev)
Open network device.
Definition: netdevice.h:222
#define list_add(new, head)
Add a new entry to the head of a list.
Definition: list.h:69
unsigned long ioaddr
I/O address.
Definition: pci.h:221
Error codes.
static u32 vp_get_features(unsigned int ioaddr)
Definition: virtio-pci.h:140
static void virtnet_process_rx_packets(struct net_device *netdev)
Complete packet reception.
Definition: virtio-net.c:382
I/O buffers.
struct pci_device_id * ids
PCI ID table.
Definition: pci.h:249
size_t mtu
Maximum transmission unit length.
Definition: netdevice.h:415
#define DBGC(...)
Definition: compiler.h:505
#define VIRTIO_PCI_CAP_PCI_CFG
Definition: virtio-pci.h:47
int vpm_find_vqs(struct virtio_pci_modern_device *vdev, unsigned nvqs, struct vring_virtqueue *vqs, struct dma_device *dma_dev, size_t header_size)
Definition: virtio-pci.c:360
char name[40]
Name.
Definition: device.h:75
#define ENOENT
No such file or directory.
Definition: errno.h:514
#define VIRTIO_NET_F_MTU
Definition: virtio-net.h:7
static __always_inline void dma_set_mask_64bit(struct dma_device *dma)
Set 64-bit addressable space mask.
Definition: dma.h:474
struct dma_device * dma
DMA device.
Definition: netdevice.h:366
static void vp_set_features(unsigned int ioaddr, u32 features)
Definition: virtio-pci.h:145
#define VIRTIO_PCI_CAP_DEVICE_CFG
Definition: virtio-pci.h:46
#define offsetof(type, field)
Get offset of a field within a structure.
Definition: stddef.h:24
#define VIRTIO_PCI_CAP_COMMON_CFG
Definition: virtio-pci.h:43
uint8_t mac[ETH_ALEN]
MAC address.
Definition: ena.h:24
static void vring_disable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:128
uint16_t device
Device ID.
Definition: ena.h:24
static void vring_enable_cb(struct vring_virtqueue *vq)
Definition: virtio-ring.h:123
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:276
void adjust_pci_device(struct pci_device *pci)
Enable PCI device.
Definition: pci.c:154
struct device dev
Generic device.
Definition: pci.h:208
__be32 out[4]
Definition: CIB_PRM.h:36
A doubly-linked list entry (or list head)
Definition: list.h:18
static int virtnet_probe_modern(struct pci_device *pci, int *found_dev)
Probe PCI device, modern virtio 1.0.
Definition: virtio-net.c:533
static void vpm_reset(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:241
uint8_t status
Status.
Definition: ena.h:16
static void netdev_init(struct net_device *netdev, struct net_device_operations *op)
Initialise a network device.
Definition: netdevice.h:515
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
static void pci_set_drvdata(struct pci_device *pci, void *priv)
Set PCI driver-private data.
Definition: pci.h:359
#define ENOMEM
Not enough space.
Definition: errno.h:534
A hardware device.
Definition: device.h:73
static unsigned long ioaddr
Definition: davicom.c:129
void vp_free_vq(struct vring_virtqueue *vq)
Definition: virtio-pci.c:48
struct virtio_pci_region device
Definition: virtio-pci.h:131
uint16_t device
Device ID.
Definition: pci.h:225
#define ETH_HLEN
Definition: if_ether.h:9
static void netdev_put(struct net_device *netdev)
Drop reference to network device.
Definition: netdevice.h:572
Ethernet protocol.
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:116
void * priv
Driver private data.
Definition: netdevice.h:431
struct dma_device * dma
DMA device.
Definition: virtio-net.c:104
static void netdev_link_up(struct net_device *netdev)
Mark network device as having link up.
Definition: netdevice.h:774
int virtio_pci_find_capability(struct pci_device *pci, uint8_t cfg_type)
Definition: virtio-pci.c:250
static struct net_device * netdev
Definition: gdbudp.c:52
uint64_t u64
Definition: stdint.h:25
static __always_inline physaddr_t iob_dma(struct io_buffer *iobuf)
Get I/O buffer DMA address.
Definition: iobuf.h:264
#define VIRTIO_F_IOMMU_PLATFORM
Definition: virtio-ring.h:24
static u8 vp_get_isr(unsigned int ioaddr)
Definition: virtio-pci.h:172
Definition: sis900.h:26
static void virtnet_close(struct net_device *netdev)
Close network device.
Definition: virtio-net.c:326
unsigned int rx_num_iobufs
Pending rx packet count.
Definition: virtio-net.c:101
void unregister_netdev(struct net_device *netdev)
Unregister network device.
Definition: netdevice.c:941
static int virtnet_open_modern(struct net_device *netdev)
Open network device, modern virtio 1.0.
Definition: virtio-net.c:250
#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:458
Linked lists.
void virtio_pci_unmap_capability(struct virtio_pci_region *region)
Definition: virtio-pci.c:346
static void vp_set_status(unsigned int ioaddr, u8 status)
Definition: virtio-pci.h:165
#define iob_unput(iobuf, len)
Definition: iobuf.h:135
physaddr_t addr
Definition: virtio-ring.h:92
struct dma_mapping map
Definition: virtio-ring.h:79
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
struct io_buffer * alloc_rx_iob(size_t len, struct dma_device *dma)
Allocate and map I/O buffer for receive DMA.
Definition: iobuf.c:181
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
struct virtio_pci_region notification
Definition: virtio-ring.h:88
PCI bus.
A PCI device.
Definition: pci.h:206
int register_netdev(struct net_device *netdev)
Register network device.
Definition: netdevice.c:759
uint32_t features
Supported features.
Definition: ena.h:16
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
const char * eth_ntoa(const void *ll_addr)
Transcribe Ethernet address.
Definition: ethernet.c:175
static void virtnet_poll(struct net_device *netdev)
Poll for completed and received packets.
Definition: virtio-net.c:412
void * vring_get_buf(struct vring_virtqueue *vq, unsigned int *len)
Definition: virtio-ring.c:62
struct virtio_pci_region common
Definition: virtio-pci.h:128
void netdev_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: netdevice.c:970
struct virtio_pci_region isr
Definition: virtio-pci.h:134
A network device.
Definition: netdevice.h:352
#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:528
#define VIRTIO_CONFIG_S_ACKNOWLEDGE
Definition: virtio-ring.h:9
#define VIRTIO_PCI_CAP_NOTIFY_CFG
Definition: virtio-pci.h:44
#define VIRTIO_F_ANY_LAYOUT
Definition: virtio-ring.h:21
int virtio_version
0 for legacy, 1 for virtio 1.0
Definition: virtio-net.c:89
#define ETH_ALEN
Definition: if_ether.h:8
struct pci_driver virtnet_driver __pci_driver
Definition: virtio-net.c:699
A PCI device ID list entry.
Definition: pci.h:170
static void virtnet_process_tx_packets(struct net_device *netdev)
Complete packet transmission.
Definition: virtio-net.c:364
#define VIRTIO_NET_F_MAC
Definition: virtio-net.h:8
static void virtnet_irq(struct net_device *netdev, int enable)
Enable or disable interrupts.
Definition: virtio-net.c:435
struct ib_cm_common common
Definition: ib_mad.h:11
static struct pci_device_id virtnet_nics[]
Definition: virtio-net.c:694
static int virtnet_probe_legacy(struct pci_device *pci)
Probe PCI device, legacy virtio 0.9.5.
Definition: virtio-net.c:462
static int is_valid_ether_addr(const void *addr)
Check if Ethernet address is valid.
Definition: ethernet.h:77
static struct xen_remove_from_physmap * remove
Definition: xenmem.h:39
static int virtnet_transmit(struct net_device *netdev, struct io_buffer *iobuf)
Transmit packet.
Definition: virtio-net.c:354
struct virtio_net_hdr_modern * empty_header
Definition: virtio-ring.h:85
Network device operations.
Definition: netdevice.h:213
void netdev_rx(struct net_device *netdev, struct io_buffer *iobuf)
Add packet to receive queue.
Definition: netdevice.c:548
struct device * dev
Underlying hardware device.
Definition: netdevice.h:364
#define EADDRNOTAVAIL
Address not available.
Definition: errno.h:308
#define VIRTIO_PCI_CAP_ISR_CFG
Definition: virtio-pci.h:45
Network device management.
static int virtnet_open(struct net_device *netdev)
Open network device.
Definition: virtio-net.c:312
static void * pci_get_drvdata(struct pci_device *pci)
Get PCI driver-private data.
Definition: pci.h:369
#define VIRTIO_CONFIG_S_DRIVER
Definition: virtio-ring.h:11
#define INIT_LIST_HEAD(list)
Initialise a list head.
Definition: list.h:45
static struct net_device_operations virtnet_operations
virtio-net device operations
Definition: virtio-net.c:448
static void vpm_add_status(struct virtio_pci_modern_device *vdev, u8 status)
Definition: virtio-pci.h:253
int vp_find_vq(unsigned int ioaddr, int queue_index, struct vring_virtqueue *vq, struct dma_device *dma_dev, size_t header_size)
Definition: virtio-pci.c:58
struct list_head list
List of which this buffer is a member.
Definition: iobuf.h:40
#define NUM_RX_BUF
Max number of pending rx packets.
Definition: virtio-net.c:82
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:98
void free_rx_iob(struct io_buffer *iobuf)
Unmap and free I/O buffer for receive DMA.
Definition: iobuf.c:208
#define DBGC2(...)
Definition: compiler.h:522
int(* probe)(struct pci_device *pci)
Probe device.
Definition: pci.h:260
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 ena_aq_header header
Header.
Definition: ena.h:12
static void vp_get(unsigned int ioaddr, unsigned offset, void *buf, unsigned len)
Definition: virtio-pci.h:150
static void virtnet_refill_rx_virtqueue(struct net_device *netdev)
Try to keep rx virtqueue filled with iobufs.
Definition: virtio-net.c:158
struct virtio_pci_modern_device vdev
Virtio 1.0 device data.
Definition: virtio-net.c:92
#define VIRTIO_CONFIG_S_FAILED
Definition: virtio-ring.h:17
static u8 vpm_get_isr(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:295
size_t max_pkt_len
Maximum packet length.
Definition: netdevice.h:409
unsigned long ioaddr
Base pio register address.
Definition: virtio-net.c:86
#define VIRTIO_CONFIG_S_DRIVER_OK
Definition: virtio-ring.h:13
#define DBG(...)
Print a debugging message.
Definition: compiler.h:498
static __always_inline physaddr_t dma(struct dma_mapping *map, void *addr)
Get DMA address from virtual address.
Definition: dma.h:436
static int virtnet_probe(struct pci_device *pci)
Probe PCI device.
Definition: virtio-net.c:666
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
static void vp_reset(unsigned int ioaddr)
Definition: virtio-pci.h:177
static u64 vpm_get_features(struct virtio_pci_modern_device *vdev)
Definition: virtio-pci.h:261
uint8_t hw_addr[MAX_HW_ADDR_LEN]
Hardware address.
Definition: netdevice.h:381
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define VIRTIO_CONFIG_S_FEATURES_OK
Definition: virtio-ring.h:15
#define PCI_ROM(_vendor, _device, _name, _description, _data)
Definition: pci.h:303
static int virtnet_open_legacy(struct net_device *netdev)
Open network device, legacy virtio 0.9.5.
Definition: virtio-net.c:203
uint8_t u8
Definition: stdint.h:19
uint32_t u32
Definition: stdint.h:23
A DMA-capable device.
Definition: dma.h:47
struct vring_virtqueue * virtqueue
RX/TX virtqueues.
Definition: virtio-net.c:95
A persistent I/O buffer.
Definition: iobuf.h:33