Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

virtio-net.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  23. #include <errno.h>
  24. #include <stdlib.h>
  25. #include <ipxe/list.h>
  26. #include <ipxe/iobuf.h>
  27. #include <ipxe/netdevice.h>
  28. #include <ipxe/pci.h>
  29. #include <ipxe/if_ether.h>
  30. #include <ipxe/ethernet.h>
  31. #include <ipxe/virtio-ring.h>
  32. #include <ipxe/virtio-pci.h>
  33. #include "virtio-net.h"
  34. /*
  35. * Virtio network device driver
  36. *
  37. * Specification:
  38. * http://ozlabs.org/~rusty/virtio-spec/
  39. *
  40. * The virtio network device is supported by Linux virtualization software
  41. * including QEMU/KVM and lguest. This driver supports the virtio over PCI
  42. * transport; virtual machines have one virtio-net PCI adapter per NIC.
  43. *
  44. * Virtio-net is different from hardware NICs because virtio devices
  45. * communicate with the hypervisor via virtqueues, not traditional descriptor
  46. * rings. Virtqueues are unordered queues, they support add_buf() and
  47. * get_buf() operations. To transmit a packet, the driver has to add the
  48. * packet buffer onto the virtqueue. To receive a packet, the driver must
  49. * first add an empty buffer to the virtqueue and then get the filled packet
  50. * buffer on completion.
  51. *
  52. * Virtqueues are an abstraction that is commonly implemented using the vring
  53. * descriptor ring layout. The vring is the actual shared memory structure
  54. * that allows the virtual machine to communicate buffers with the hypervisor.
  55. * Because the vring layout is optimized for flexibility and performance rather
  56. * than space, it is heavy-weight and allocated like traditional descriptor
  57. * rings in the open() function of the driver and not in probe().
  58. *
  59. * There is no true interrupt enable/disable. Virtqueues have callback
  60. * enable/disable flags but these are only hints. The hypervisor may still
  61. * raise an interrupt. Nevertheless, this driver disables callbacks in the
  62. * hopes of avoiding interrupts.
  63. */
  64. /* Driver types are declared here so virtio-net.h can be easily synced with its
  65. * Linux source.
  66. */
  67. /* Virtqueue indices */
  68. enum {
  69. RX_INDEX = 0,
  70. TX_INDEX,
  71. QUEUE_NB
  72. };
  73. enum {
  74. /** Max number of pending rx packets */
  75. NUM_RX_BUF = 8,
  76. /** Max Ethernet frame length, including FCS and VLAN tag */
  77. RX_BUF_SIZE = 1522,
  78. };
  79. struct virtnet_nic {
  80. /** Base pio register address */
  81. unsigned long ioaddr;
  82. /** RX/TX virtqueues */
  83. struct vring_virtqueue *virtqueue;
  84. /** RX packets handed to the NIC waiting to be filled in */
  85. struct list_head rx_iobufs;
  86. /** Pending rx packet count */
  87. unsigned int rx_num_iobufs;
  88. /** Virtio net packet header, we only need one */
  89. struct virtio_net_hdr empty_header;
  90. };
  91. /** Add an iobuf to a virtqueue
  92. *
  93. * @v netdev Network device
  94. * @v vq_idx Virtqueue index (RX_INDEX or TX_INDEX)
  95. * @v iobuf I/O buffer
  96. *
  97. * The virtqueue is kicked after the iobuf has been added.
  98. */
  99. static void virtnet_enqueue_iob ( struct net_device *netdev,
  100. int vq_idx, struct io_buffer *iobuf ) {
  101. struct virtnet_nic *virtnet = netdev->priv;
  102. struct vring_virtqueue *vq = &virtnet->virtqueue[vq_idx];
  103. unsigned int out = ( vq_idx == TX_INDEX ) ? 2 : 0;
  104. unsigned int in = ( vq_idx == TX_INDEX ) ? 0 : 2;
  105. struct vring_list list[] = {
  106. {
  107. /* Share a single zeroed virtio net header between all
  108. * rx and tx packets. This works because this driver
  109. * does not use any advanced features so none of the
  110. * header fields get used.
  111. */
  112. .addr = ( char* ) &virtnet->empty_header,
  113. .length = sizeof ( virtnet->empty_header ),
  114. },
  115. {
  116. .addr = ( char* ) iobuf->data,
  117. .length = iob_len ( iobuf ),
  118. },
  119. };
  120. DBGC ( virtnet, "VIRTIO-NET %p enqueuing iobuf %p on vq %d\n",
  121. virtnet, iobuf, vq_idx );
  122. vring_add_buf ( vq, list, out, in, iobuf, 0 );
  123. vring_kick ( virtnet->ioaddr, vq, 1 );
  124. }
  125. /** Try to keep rx virtqueue filled with iobufs
  126. *
  127. * @v netdev Network device
  128. */
  129. static void virtnet_refill_rx_virtqueue ( struct net_device *netdev ) {
  130. struct virtnet_nic *virtnet = netdev->priv;
  131. while ( virtnet->rx_num_iobufs < NUM_RX_BUF ) {
  132. struct io_buffer *iobuf;
  133. /* Try to allocate a buffer, stop for now if out of memory */
  134. iobuf = alloc_iob ( RX_BUF_SIZE );
  135. if ( ! iobuf )
  136. break;
  137. /* Keep track of iobuf so close() can free it */
  138. list_add ( &iobuf->list, &virtnet->rx_iobufs );
  139. /* Mark packet length until we know the actual size */
  140. iob_put ( iobuf, RX_BUF_SIZE );
  141. virtnet_enqueue_iob ( netdev, RX_INDEX, iobuf );
  142. virtnet->rx_num_iobufs++;
  143. }
  144. }
  145. /** Open network device
  146. *
  147. * @v netdev Network device
  148. * @ret rc Return status code
  149. */
  150. static int virtnet_open ( struct net_device *netdev ) {
  151. struct virtnet_nic *virtnet = netdev->priv;
  152. unsigned long ioaddr = virtnet->ioaddr;
  153. u32 features;
  154. int i;
  155. /* Reset for sanity */
  156. vp_reset ( ioaddr );
  157. /* Allocate virtqueues */
  158. virtnet->virtqueue = zalloc ( QUEUE_NB *
  159. sizeof ( *virtnet->virtqueue ) );
  160. if ( ! virtnet->virtqueue )
  161. return -ENOMEM;
  162. /* Initialize rx/tx virtqueues */
  163. for ( i = 0; i < QUEUE_NB; i++ ) {
  164. if ( vp_find_vq ( ioaddr, i, &virtnet->virtqueue[i] ) == -1 ) {
  165. DBGC ( virtnet, "VIRTIO-NET %p cannot register queue %d\n",
  166. virtnet, i );
  167. free ( virtnet->virtqueue );
  168. virtnet->virtqueue = NULL;
  169. return -ENOENT;
  170. }
  171. }
  172. /* Initialize rx packets */
  173. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  174. virtnet->rx_num_iobufs = 0;
  175. virtnet_refill_rx_virtqueue ( netdev );
  176. /* Disable interrupts before starting */
  177. netdev_irq ( netdev, 0 );
  178. /* Driver is ready */
  179. features = vp_get_features ( ioaddr );
  180. vp_set_features ( ioaddr, features & ( 1 << VIRTIO_NET_F_MAC ) );
  181. vp_set_status ( ioaddr, VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK );
  182. return 0;
  183. }
  184. /** Close network device
  185. *
  186. * @v netdev Network device
  187. */
  188. static void virtnet_close ( struct net_device *netdev ) {
  189. struct virtnet_nic *virtnet = netdev->priv;
  190. struct io_buffer *iobuf;
  191. struct io_buffer *next_iobuf;
  192. vp_reset ( virtnet->ioaddr );
  193. /* Virtqueues can be freed now that NIC is reset */
  194. free ( virtnet->virtqueue );
  195. virtnet->virtqueue = NULL;
  196. /* Free rx iobufs */
  197. list_for_each_entry_safe ( iobuf, next_iobuf, &virtnet->rx_iobufs, list ) {
  198. free_iob ( iobuf );
  199. }
  200. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  201. virtnet->rx_num_iobufs = 0;
  202. }
  203. /** Transmit packet
  204. *
  205. * @v netdev Network device
  206. * @v iobuf I/O buffer
  207. * @ret rc Return status code
  208. */
  209. static int virtnet_transmit ( struct net_device *netdev,
  210. struct io_buffer *iobuf ) {
  211. virtnet_enqueue_iob ( netdev, TX_INDEX, iobuf );
  212. return 0;
  213. }
  214. /** Complete packet transmission
  215. *
  216. * @v netdev Network device
  217. */
  218. static void virtnet_process_tx_packets ( struct net_device *netdev ) {
  219. struct virtnet_nic *virtnet = netdev->priv;
  220. struct vring_virtqueue *tx_vq = &virtnet->virtqueue[TX_INDEX];
  221. while ( vring_more_used ( tx_vq ) ) {
  222. struct io_buffer *iobuf = vring_get_buf ( tx_vq, NULL );
  223. DBGC ( virtnet, "VIRTIO-NET %p tx complete iobuf %p\n",
  224. virtnet, iobuf );
  225. netdev_tx_complete ( netdev, iobuf );
  226. }
  227. }
  228. /** Complete packet reception
  229. *
  230. * @v netdev Network device
  231. */
  232. static void virtnet_process_rx_packets ( struct net_device *netdev ) {
  233. struct virtnet_nic *virtnet = netdev->priv;
  234. struct vring_virtqueue *rx_vq = &virtnet->virtqueue[RX_INDEX];
  235. while ( vring_more_used ( rx_vq ) ) {
  236. unsigned int len;
  237. struct io_buffer *iobuf = vring_get_buf ( rx_vq, &len );
  238. /* Release ownership of iobuf */
  239. list_del ( &iobuf->list );
  240. virtnet->rx_num_iobufs--;
  241. /* Update iobuf length */
  242. iob_unput ( iobuf, RX_BUF_SIZE );
  243. iob_put ( iobuf, len - sizeof ( struct virtio_net_hdr ) );
  244. DBGC ( virtnet, "VIRTIO-NET %p rx complete iobuf %p len %zd\n",
  245. virtnet, iobuf, iob_len ( iobuf ) );
  246. /* Pass completed packet to the network stack */
  247. netdev_rx ( netdev, iobuf );
  248. }
  249. virtnet_refill_rx_virtqueue ( netdev );
  250. }
  251. /** Poll for completed and received packets
  252. *
  253. * @v netdev Network device
  254. */
  255. static void virtnet_poll ( struct net_device *netdev ) {
  256. struct virtnet_nic *virtnet = netdev->priv;
  257. /* Acknowledge interrupt. This is necessary for UNDI operation and
  258. * interrupts that are raised despite VRING_AVAIL_F_NO_INTERRUPT being
  259. * set (that flag is just a hint and the hypervisor not not have to
  260. * honor it).
  261. */
  262. vp_get_isr ( virtnet->ioaddr );
  263. virtnet_process_tx_packets ( netdev );
  264. virtnet_process_rx_packets ( netdev );
  265. }
  266. /** Enable or disable interrupts
  267. *
  268. * @v netdev Network device
  269. * @v enable Interrupts should be enabled
  270. */
  271. static void virtnet_irq ( struct net_device *netdev, int enable ) {
  272. struct virtnet_nic *virtnet = netdev->priv;
  273. int i;
  274. for ( i = 0; i < QUEUE_NB; i++ ) {
  275. if ( enable )
  276. vring_enable_cb ( &virtnet->virtqueue[i] );
  277. else
  278. vring_disable_cb ( &virtnet->virtqueue[i] );
  279. }
  280. }
  281. /** virtio-net device operations */
  282. static struct net_device_operations virtnet_operations = {
  283. .open = virtnet_open,
  284. .close = virtnet_close,
  285. .transmit = virtnet_transmit,
  286. .poll = virtnet_poll,
  287. .irq = virtnet_irq,
  288. };
  289. /**
  290. * Probe PCI device
  291. *
  292. * @v pci PCI device
  293. * @v id PCI ID
  294. * @ret rc Return status code
  295. */
  296. static int virtnet_probe ( struct pci_device *pci ) {
  297. unsigned long ioaddr = pci->ioaddr;
  298. struct net_device *netdev;
  299. struct virtnet_nic *virtnet;
  300. u32 features;
  301. int rc;
  302. /* Allocate and hook up net device */
  303. netdev = alloc_etherdev ( sizeof ( *virtnet ) );
  304. if ( ! netdev )
  305. return -ENOMEM;
  306. netdev_init ( netdev, &virtnet_operations );
  307. virtnet = netdev->priv;
  308. virtnet->ioaddr = ioaddr;
  309. pci_set_drvdata ( pci, netdev );
  310. netdev->dev = &pci->dev;
  311. DBGC ( virtnet, "VIRTIO-NET %p busaddr=%s ioaddr=%#lx irq=%d\n",
  312. virtnet, pci->dev.name, ioaddr, pci->irq );
  313. /* Enable PCI bus master and reset NIC */
  314. adjust_pci_device ( pci );
  315. vp_reset ( ioaddr );
  316. /* Load MAC address */
  317. features = vp_get_features ( ioaddr );
  318. if ( features & ( 1 << VIRTIO_NET_F_MAC ) ) {
  319. vp_get ( ioaddr, offsetof ( struct virtio_net_config, mac ),
  320. netdev->hw_addr, ETH_ALEN );
  321. DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
  322. eth_ntoa ( netdev->hw_addr ) );
  323. }
  324. /* Register network device */
  325. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  326. goto err_register_netdev;
  327. /* Mark link as up, control virtqueue is not used */
  328. netdev_link_up ( netdev );
  329. return 0;
  330. unregister_netdev ( netdev );
  331. err_register_netdev:
  332. vp_reset ( ioaddr );
  333. netdev_nullify ( netdev );
  334. netdev_put ( netdev );
  335. return rc;
  336. }
  337. /**
  338. * Remove device
  339. *
  340. * @v pci PCI device
  341. */
  342. static void virtnet_remove ( struct pci_device *pci ) {
  343. struct net_device *netdev = pci_get_drvdata ( pci );
  344. unregister_netdev ( netdev );
  345. netdev_nullify ( netdev );
  346. netdev_put ( netdev );
  347. }
  348. static struct pci_device_id virtnet_nics[] = {
  349. PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface", 0),
  350. };
  351. struct pci_driver virtnet_driver __pci_driver = {
  352. .ids = virtnet_nics,
  353. .id_count = ( sizeof ( virtnet_nics ) / sizeof ( virtnet_nics[0] ) ),
  354. .probe = virtnet_probe,
  355. .remove = virtnet_remove,
  356. };