You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

virtio-net.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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 <unistd.h>
  26. #include <ipxe/list.h>
  27. #include <ipxe/iobuf.h>
  28. #include <ipxe/netdevice.h>
  29. #include <ipxe/pci.h>
  30. #include <ipxe/if_ether.h>
  31. #include <ipxe/ethernet.h>
  32. #include <ipxe/virtio-pci.h>
  33. #include <ipxe/virtio-ring.h>
  34. #include "virtio-net.h"
  35. /*
  36. * Virtio network device driver
  37. *
  38. * Specification:
  39. * http://ozlabs.org/~rusty/virtio-spec/
  40. *
  41. * The virtio network device is supported by Linux virtualization software
  42. * including QEMU/KVM and lguest. This driver supports the virtio over PCI
  43. * transport; virtual machines have one virtio-net PCI adapter per NIC.
  44. *
  45. * Virtio-net is different from hardware NICs because virtio devices
  46. * communicate with the hypervisor via virtqueues, not traditional descriptor
  47. * rings. Virtqueues are unordered queues, they support add_buf() and
  48. * get_buf() operations. To transmit a packet, the driver has to add the
  49. * packet buffer onto the virtqueue. To receive a packet, the driver must
  50. * first add an empty buffer to the virtqueue and then get the filled packet
  51. * buffer on completion.
  52. *
  53. * Virtqueues are an abstraction that is commonly implemented using the vring
  54. * descriptor ring layout. The vring is the actual shared memory structure
  55. * that allows the virtual machine to communicate buffers with the hypervisor.
  56. * Because the vring layout is optimized for flexibility and performance rather
  57. * than space, it is heavy-weight and allocated like traditional descriptor
  58. * rings in the open() function of the driver and not in probe().
  59. *
  60. * There is no true interrupt enable/disable. Virtqueues have callback
  61. * enable/disable flags but these are only hints. The hypervisor may still
  62. * raise an interrupt. Nevertheless, this driver disables callbacks in the
  63. * hopes of avoiding interrupts.
  64. */
  65. /* Driver types are declared here so virtio-net.h can be easily synced with its
  66. * Linux source.
  67. */
  68. /* Virtqueue indices */
  69. enum {
  70. RX_INDEX = 0,
  71. TX_INDEX,
  72. QUEUE_NB
  73. };
  74. enum {
  75. /** Max number of pending rx packets */
  76. NUM_RX_BUF = 8,
  77. /** Max Ethernet frame length, including FCS and VLAN tag */
  78. RX_BUF_SIZE = 1522,
  79. };
  80. struct virtnet_nic {
  81. /** Base pio register address */
  82. unsigned long ioaddr;
  83. /** 0 for legacy, 1 for virtio 1.0 */
  84. int virtio_version;
  85. /** Virtio 1.0 device data */
  86. struct virtio_pci_modern_device vdev;
  87. /** RX/TX virtqueues */
  88. struct vring_virtqueue *virtqueue;
  89. /** RX packets handed to the NIC waiting to be filled in */
  90. struct list_head rx_iobufs;
  91. /** Pending rx packet count */
  92. unsigned int rx_num_iobufs;
  93. /** Virtio net packet header, we only need one */
  94. struct virtio_net_hdr_modern empty_header;
  95. };
  96. /** Add an iobuf to a virtqueue
  97. *
  98. * @v netdev Network device
  99. * @v vq_idx Virtqueue index (RX_INDEX or TX_INDEX)
  100. * @v iobuf I/O buffer
  101. *
  102. * The virtqueue is kicked after the iobuf has been added.
  103. */
  104. static void virtnet_enqueue_iob ( struct net_device *netdev,
  105. int vq_idx, struct io_buffer *iobuf ) {
  106. struct virtnet_nic *virtnet = netdev->priv;
  107. struct vring_virtqueue *vq = &virtnet->virtqueue[vq_idx];
  108. unsigned int out = ( vq_idx == TX_INDEX ) ? 2 : 0;
  109. unsigned int in = ( vq_idx == TX_INDEX ) ? 0 : 2;
  110. size_t header_len = virtnet->virtio_version
  111. ? sizeof ( virtnet->empty_header )
  112. : sizeof ( virtnet->empty_header.legacy );
  113. struct vring_list list[] = {
  114. {
  115. /* Share a single zeroed virtio net header between all
  116. * rx and tx packets. This works because this driver
  117. * does not use any advanced features so none of the
  118. * header fields get used.
  119. */
  120. .addr = ( char* ) &virtnet->empty_header,
  121. .length = header_len,
  122. },
  123. {
  124. .addr = ( char* ) iobuf->data,
  125. .length = iob_len ( iobuf ),
  126. },
  127. };
  128. DBGC2 ( virtnet, "VIRTIO-NET %p enqueuing iobuf %p on vq %d\n",
  129. virtnet, iobuf, vq_idx );
  130. vring_add_buf ( vq, list, out, in, iobuf, 0 );
  131. vring_kick ( virtnet->virtio_version ? &virtnet->vdev : NULL,
  132. virtnet->ioaddr, vq, 1 );
  133. }
  134. /** Try to keep rx virtqueue filled with iobufs
  135. *
  136. * @v netdev Network device
  137. */
  138. static void virtnet_refill_rx_virtqueue ( struct net_device *netdev ) {
  139. struct virtnet_nic *virtnet = netdev->priv;
  140. while ( virtnet->rx_num_iobufs < NUM_RX_BUF ) {
  141. struct io_buffer *iobuf;
  142. /* Try to allocate a buffer, stop for now if out of memory */
  143. iobuf = alloc_iob ( RX_BUF_SIZE );
  144. if ( ! iobuf )
  145. break;
  146. /* Keep track of iobuf so close() can free it */
  147. list_add ( &iobuf->list, &virtnet->rx_iobufs );
  148. /* Mark packet length until we know the actual size */
  149. iob_put ( iobuf, RX_BUF_SIZE );
  150. virtnet_enqueue_iob ( netdev, RX_INDEX, iobuf );
  151. virtnet->rx_num_iobufs++;
  152. }
  153. }
  154. /** Open network device, legacy virtio 0.9.5
  155. *
  156. * @v netdev Network device
  157. * @ret rc Return status code
  158. */
  159. static int virtnet_open_legacy ( struct net_device *netdev ) {
  160. struct virtnet_nic *virtnet = netdev->priv;
  161. unsigned long ioaddr = virtnet->ioaddr;
  162. u32 features;
  163. int i;
  164. /* Reset for sanity */
  165. vp_reset ( ioaddr );
  166. /* Allocate virtqueues */
  167. virtnet->virtqueue = zalloc ( QUEUE_NB *
  168. sizeof ( *virtnet->virtqueue ) );
  169. if ( ! virtnet->virtqueue )
  170. return -ENOMEM;
  171. /* Initialize rx/tx virtqueues */
  172. for ( i = 0; i < QUEUE_NB; i++ ) {
  173. if ( vp_find_vq ( ioaddr, i, &virtnet->virtqueue[i] ) == -1 ) {
  174. DBGC ( virtnet, "VIRTIO-NET %p cannot register queue %d\n",
  175. virtnet, i );
  176. free ( virtnet->virtqueue );
  177. virtnet->virtqueue = NULL;
  178. return -ENOENT;
  179. }
  180. }
  181. /* Initialize rx packets */
  182. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  183. virtnet->rx_num_iobufs = 0;
  184. virtnet_refill_rx_virtqueue ( netdev );
  185. /* Disable interrupts before starting */
  186. netdev_irq ( netdev, 0 );
  187. /* Driver is ready */
  188. features = vp_get_features ( ioaddr );
  189. vp_set_features ( ioaddr, features & ( 1 << VIRTIO_NET_F_MAC ) );
  190. vp_set_status ( ioaddr, VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK );
  191. return 0;
  192. }
  193. /** Open network device, modern virtio 1.0
  194. *
  195. * @v netdev Network device
  196. * @ret rc Return status code
  197. */
  198. static int virtnet_open_modern ( struct net_device *netdev ) {
  199. struct virtnet_nic *virtnet = netdev->priv;
  200. u64 features;
  201. u8 status;
  202. /* Negotiate features */
  203. features = vpm_get_features ( &virtnet->vdev );
  204. if ( ! ( features & VIRTIO_F_VERSION_1 ) ) {
  205. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  206. return -EINVAL;
  207. }
  208. vpm_set_features ( &virtnet->vdev, features & (
  209. ( 1ULL << VIRTIO_NET_F_MAC ) |
  210. ( 1ULL << VIRTIO_F_VERSION_1 ) |
  211. ( 1ULL << VIRTIO_F_ANY_LAYOUT ) ) );
  212. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FEATURES_OK );
  213. status = vpm_get_status ( &virtnet->vdev );
  214. if ( ! ( status & VIRTIO_CONFIG_S_FEATURES_OK ) ) {
  215. DBGC ( virtnet, "VIRTIO-NET %p device didn't accept features\n",
  216. virtnet );
  217. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  218. return -EINVAL;
  219. }
  220. /* Allocate virtqueues */
  221. virtnet->virtqueue = zalloc ( QUEUE_NB *
  222. sizeof ( *virtnet->virtqueue ) );
  223. if ( ! virtnet->virtqueue ) {
  224. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  225. return -ENOMEM;
  226. }
  227. /* Initialize rx/tx virtqueues */
  228. if ( vpm_find_vqs ( &virtnet->vdev, QUEUE_NB, virtnet->virtqueue ) ) {
  229. DBGC ( virtnet, "VIRTIO-NET %p cannot register queues\n",
  230. virtnet );
  231. free ( virtnet->virtqueue );
  232. virtnet->virtqueue = NULL;
  233. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  234. return -ENOENT;
  235. }
  236. /* Disable interrupts before starting */
  237. netdev_irq ( netdev, 0 );
  238. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_DRIVER_OK );
  239. /* Initialize rx packets */
  240. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  241. virtnet->rx_num_iobufs = 0;
  242. virtnet_refill_rx_virtqueue ( netdev );
  243. return 0;
  244. }
  245. /** Open network device
  246. *
  247. * @v netdev Network device
  248. * @ret rc Return status code
  249. */
  250. static int virtnet_open ( struct net_device *netdev ) {
  251. struct virtnet_nic *virtnet = netdev->priv;
  252. if ( virtnet->virtio_version ) {
  253. return virtnet_open_modern ( netdev );
  254. } else {
  255. return virtnet_open_legacy ( netdev );
  256. }
  257. }
  258. /** Close network device
  259. *
  260. * @v netdev Network device
  261. */
  262. static void virtnet_close ( struct net_device *netdev ) {
  263. struct virtnet_nic *virtnet = netdev->priv;
  264. struct io_buffer *iobuf;
  265. struct io_buffer *next_iobuf;
  266. int i;
  267. if ( virtnet->virtio_version ) {
  268. vpm_reset ( &virtnet->vdev );
  269. } else {
  270. vp_reset ( virtnet->ioaddr );
  271. }
  272. /* Virtqueues can be freed now that NIC is reset */
  273. for ( i = 0 ; i < QUEUE_NB ; i++ ) {
  274. virtio_pci_unmap_capability ( &virtnet->virtqueue[i].notification );
  275. }
  276. free ( virtnet->virtqueue );
  277. virtnet->virtqueue = NULL;
  278. /* Free rx iobufs */
  279. list_for_each_entry_safe ( iobuf, next_iobuf, &virtnet->rx_iobufs, list ) {
  280. free_iob ( iobuf );
  281. }
  282. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  283. virtnet->rx_num_iobufs = 0;
  284. }
  285. /** Transmit packet
  286. *
  287. * @v netdev Network device
  288. * @v iobuf I/O buffer
  289. * @ret rc Return status code
  290. */
  291. static int virtnet_transmit ( struct net_device *netdev,
  292. struct io_buffer *iobuf ) {
  293. virtnet_enqueue_iob ( netdev, TX_INDEX, iobuf );
  294. return 0;
  295. }
  296. /** Complete packet transmission
  297. *
  298. * @v netdev Network device
  299. */
  300. static void virtnet_process_tx_packets ( struct net_device *netdev ) {
  301. struct virtnet_nic *virtnet = netdev->priv;
  302. struct vring_virtqueue *tx_vq = &virtnet->virtqueue[TX_INDEX];
  303. while ( vring_more_used ( tx_vq ) ) {
  304. struct io_buffer *iobuf = vring_get_buf ( tx_vq, NULL );
  305. DBGC2 ( virtnet, "VIRTIO-NET %p tx complete iobuf %p\n",
  306. virtnet, iobuf );
  307. netdev_tx_complete ( netdev, iobuf );
  308. }
  309. }
  310. /** Complete packet reception
  311. *
  312. * @v netdev Network device
  313. */
  314. static void virtnet_process_rx_packets ( struct net_device *netdev ) {
  315. struct virtnet_nic *virtnet = netdev->priv;
  316. struct vring_virtqueue *rx_vq = &virtnet->virtqueue[RX_INDEX];
  317. while ( vring_more_used ( rx_vq ) ) {
  318. unsigned int len;
  319. struct io_buffer *iobuf = vring_get_buf ( rx_vq, &len );
  320. /* Release ownership of iobuf */
  321. list_del ( &iobuf->list );
  322. virtnet->rx_num_iobufs--;
  323. /* Update iobuf length */
  324. iob_unput ( iobuf, RX_BUF_SIZE );
  325. iob_put ( iobuf, len - sizeof ( struct virtio_net_hdr ) );
  326. DBGC2 ( virtnet, "VIRTIO-NET %p rx complete iobuf %p len %zd\n",
  327. virtnet, iobuf, iob_len ( iobuf ) );
  328. /* Pass completed packet to the network stack */
  329. netdev_rx ( netdev, iobuf );
  330. }
  331. virtnet_refill_rx_virtqueue ( netdev );
  332. }
  333. /** Poll for completed and received packets
  334. *
  335. * @v netdev Network device
  336. */
  337. static void virtnet_poll ( struct net_device *netdev ) {
  338. struct virtnet_nic *virtnet = netdev->priv;
  339. /* Acknowledge interrupt. This is necessary for UNDI operation and
  340. * interrupts that are raised despite VRING_AVAIL_F_NO_INTERRUPT being
  341. * set (that flag is just a hint and the hypervisor does not have to
  342. * honor it).
  343. */
  344. if ( virtnet->virtio_version ) {
  345. vpm_get_isr ( &virtnet->vdev );
  346. } else {
  347. vp_get_isr ( virtnet->ioaddr );
  348. }
  349. virtnet_process_tx_packets ( netdev );
  350. virtnet_process_rx_packets ( netdev );
  351. }
  352. /** Enable or disable interrupts
  353. *
  354. * @v netdev Network device
  355. * @v enable Interrupts should be enabled
  356. */
  357. static void virtnet_irq ( struct net_device *netdev, int enable ) {
  358. struct virtnet_nic *virtnet = netdev->priv;
  359. int i;
  360. for ( i = 0; i < QUEUE_NB; i++ ) {
  361. if ( enable )
  362. vring_enable_cb ( &virtnet->virtqueue[i] );
  363. else
  364. vring_disable_cb ( &virtnet->virtqueue[i] );
  365. }
  366. }
  367. /** virtio-net device operations */
  368. static struct net_device_operations virtnet_operations = {
  369. .open = virtnet_open,
  370. .close = virtnet_close,
  371. .transmit = virtnet_transmit,
  372. .poll = virtnet_poll,
  373. .irq = virtnet_irq,
  374. };
  375. /**
  376. * Probe PCI device, legacy virtio 0.9.5
  377. *
  378. * @v pci PCI device
  379. * @ret rc Return status code
  380. */
  381. static int virtnet_probe_legacy ( struct pci_device *pci ) {
  382. unsigned long ioaddr = pci->ioaddr;
  383. struct net_device *netdev;
  384. struct virtnet_nic *virtnet;
  385. u32 features;
  386. int rc;
  387. /* Allocate and hook up net device */
  388. netdev = alloc_etherdev ( sizeof ( *virtnet ) );
  389. if ( ! netdev )
  390. return -ENOMEM;
  391. netdev_init ( netdev, &virtnet_operations );
  392. virtnet = netdev->priv;
  393. virtnet->ioaddr = ioaddr;
  394. pci_set_drvdata ( pci, netdev );
  395. netdev->dev = &pci->dev;
  396. DBGC ( virtnet, "VIRTIO-NET %p busaddr=%s ioaddr=%#lx irq=%d\n",
  397. virtnet, pci->dev.name, ioaddr, pci->irq );
  398. /* Enable PCI bus master and reset NIC */
  399. adjust_pci_device ( pci );
  400. vp_reset ( ioaddr );
  401. /* Load MAC address */
  402. features = vp_get_features ( ioaddr );
  403. if ( features & ( 1 << VIRTIO_NET_F_MAC ) ) {
  404. vp_get ( ioaddr, offsetof ( struct virtio_net_config, mac ),
  405. netdev->hw_addr, ETH_ALEN );
  406. DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
  407. eth_ntoa ( netdev->hw_addr ) );
  408. }
  409. /* Register network device */
  410. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  411. goto err_register_netdev;
  412. /* Mark link as up, control virtqueue is not used */
  413. netdev_link_up ( netdev );
  414. return 0;
  415. unregister_netdev ( netdev );
  416. err_register_netdev:
  417. vp_reset ( ioaddr );
  418. netdev_nullify ( netdev );
  419. netdev_put ( netdev );
  420. return rc;
  421. }
  422. /**
  423. * Probe PCI device, modern virtio 1.0
  424. *
  425. * @v pci PCI device
  426. * @v found_dev Set to non-zero if modern device was found (probe may still fail)
  427. * @ret rc Return status code
  428. */
  429. static int virtnet_probe_modern ( struct pci_device *pci, int *found_dev ) {
  430. struct net_device *netdev;
  431. struct virtnet_nic *virtnet;
  432. u64 features;
  433. int rc, common, isr, notify, config, device;
  434. common = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_COMMON_CFG );
  435. if ( ! common ) {
  436. DBG ( "Common virtio capability not found!\n" );
  437. return -ENODEV;
  438. }
  439. *found_dev = 1;
  440. isr = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_ISR_CFG );
  441. notify = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_NOTIFY_CFG );
  442. config = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_PCI_CFG );
  443. if ( ! isr || ! notify || ! config ) {
  444. DBG ( "Missing virtio capabilities %i/%i/%i/%i\n",
  445. common, isr, notify, config );
  446. return -EINVAL;
  447. }
  448. device = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_DEVICE_CFG );
  449. /* Allocate and hook up net device */
  450. netdev = alloc_etherdev ( sizeof ( *virtnet ) );
  451. if ( ! netdev )
  452. return -ENOMEM;
  453. netdev_init ( netdev, &virtnet_operations );
  454. virtnet = netdev->priv;
  455. pci_set_drvdata ( pci, netdev );
  456. netdev->dev = &pci->dev;
  457. DBGC ( virtnet, "VIRTIO-NET modern %p busaddr=%s irq=%d\n",
  458. virtnet, pci->dev.name, pci->irq );
  459. virtnet->vdev.pci = pci;
  460. rc = virtio_pci_map_capability ( pci, common,
  461. sizeof ( struct virtio_pci_common_cfg ), 4,
  462. 0, sizeof ( struct virtio_pci_common_cfg ),
  463. &virtnet->vdev.common );
  464. if ( rc )
  465. goto err_map_common;
  466. rc = virtio_pci_map_capability ( pci, isr, sizeof ( u8 ), 1,
  467. 0, 1,
  468. &virtnet->vdev.isr );
  469. if ( rc )
  470. goto err_map_isr;
  471. virtnet->vdev.notify_cap_pos = notify;
  472. virtnet->vdev.cfg_cap_pos = config;
  473. /* Map the device capability */
  474. if ( device ) {
  475. rc = virtio_pci_map_capability ( pci, device,
  476. 0, 4, 0, sizeof ( struct virtio_net_config ),
  477. &virtnet->vdev.device );
  478. if ( rc )
  479. goto err_map_device;
  480. }
  481. /* Enable the PCI device */
  482. adjust_pci_device ( pci );
  483. /* Reset the device and set initial status bits */
  484. vpm_reset ( &virtnet->vdev );
  485. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE );
  486. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_DRIVER );
  487. /* Load MAC address */
  488. if ( device ) {
  489. features = vpm_get_features ( &virtnet->vdev );
  490. if ( features & ( 1ULL << VIRTIO_NET_F_MAC ) ) {
  491. vpm_get ( &virtnet->vdev,
  492. offsetof ( struct virtio_net_config, mac ),
  493. netdev->hw_addr, ETH_ALEN );
  494. DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
  495. eth_ntoa ( netdev->hw_addr ) );
  496. }
  497. }
  498. /* We need a valid MAC address */
  499. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  500. rc = -EADDRNOTAVAIL;
  501. goto err_mac_address;
  502. }
  503. /* Register network device */
  504. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  505. goto err_register_netdev;
  506. /* Mark link as up, control virtqueue is not used */
  507. netdev_link_up ( netdev );
  508. virtnet->virtio_version = 1;
  509. return 0;
  510. unregister_netdev ( netdev );
  511. err_register_netdev:
  512. err_mac_address:
  513. vpm_reset ( &virtnet->vdev );
  514. netdev_nullify ( netdev );
  515. netdev_put ( netdev );
  516. virtio_pci_unmap_capability ( &virtnet->vdev.device );
  517. err_map_device:
  518. virtio_pci_unmap_capability ( &virtnet->vdev.isr );
  519. err_map_isr:
  520. virtio_pci_unmap_capability ( &virtnet->vdev.common );
  521. err_map_common:
  522. return rc;
  523. }
  524. /**
  525. * Probe PCI device
  526. *
  527. * @v pci PCI device
  528. * @ret rc Return status code
  529. */
  530. static int virtnet_probe ( struct pci_device *pci ) {
  531. int found_modern = 0;
  532. int rc = virtnet_probe_modern ( pci, &found_modern );
  533. if ( ! found_modern && pci->device < 0x1040 ) {
  534. /* fall back to the legacy probe */
  535. rc = virtnet_probe_legacy ( pci );
  536. }
  537. return rc;
  538. }
  539. /**
  540. * Remove device
  541. *
  542. * @v pci PCI device
  543. */
  544. static void virtnet_remove ( struct pci_device *pci ) {
  545. struct net_device *netdev = pci_get_drvdata ( pci );
  546. struct virtnet_nic *virtnet = netdev->priv;
  547. virtio_pci_unmap_capability ( &virtnet->vdev.device );
  548. virtio_pci_unmap_capability ( &virtnet->vdev.isr );
  549. virtio_pci_unmap_capability ( &virtnet->vdev.common );
  550. unregister_netdev ( netdev );
  551. netdev_nullify ( netdev );
  552. netdev_put ( netdev );
  553. }
  554. static struct pci_device_id virtnet_nics[] = {
  555. PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface", 0),
  556. PCI_ROM(0x1af4, 0x1041, "virtio-net", "Virtio Network Interface 1.0", 0),
  557. };
  558. struct pci_driver virtnet_driver __pci_driver = {
  559. .ids = virtnet_nics,
  560. .id_count = ( sizeof ( virtnet_nics ) / sizeof ( virtnet_nics[0] ) ),
  561. .probe = virtnet_probe,
  562. .remove = virtnet_remove,
  563. };