Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

virtio-net.c 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 dummy packet headers */
  94. struct virtio_net_hdr_modern empty_header[QUEUE_NB];
  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. struct virtio_net_hdr_modern *header = &virtnet->empty_header[vq_idx];
  109. unsigned int out = ( vq_idx == TX_INDEX ) ? 2 : 0;
  110. unsigned int in = ( vq_idx == TX_INDEX ) ? 0 : 2;
  111. size_t header_len = ( virtnet->virtio_version ?
  112. sizeof ( *header ) : sizeof ( header->legacy ) );
  113. struct vring_list list[] = {
  114. {
  115. /* Share a single zeroed virtio net header between all
  116. * packets in a ring. This works because this driver
  117. * does not use any advanced features so none of the
  118. * header fields get used.
  119. *
  120. * Some host implementations (notably Google Compute
  121. * Platform) are known to unconditionally write back
  122. * to header->flags for received packets. Work around
  123. * this by using separate RX and TX headers.
  124. */
  125. .addr = ( char* ) header,
  126. .length = header_len,
  127. },
  128. {
  129. .addr = ( char* ) iobuf->data,
  130. .length = iob_len ( iobuf ),
  131. },
  132. };
  133. DBGC2 ( virtnet, "VIRTIO-NET %p enqueuing iobuf %p on vq %d\n",
  134. virtnet, iobuf, vq_idx );
  135. vring_add_buf ( vq, list, out, in, iobuf, 0 );
  136. vring_kick ( virtnet->virtio_version ? &virtnet->vdev : NULL,
  137. virtnet->ioaddr, vq, 1 );
  138. }
  139. /** Try to keep rx virtqueue filled with iobufs
  140. *
  141. * @v netdev Network device
  142. */
  143. static void virtnet_refill_rx_virtqueue ( struct net_device *netdev ) {
  144. struct virtnet_nic *virtnet = netdev->priv;
  145. while ( virtnet->rx_num_iobufs < NUM_RX_BUF ) {
  146. struct io_buffer *iobuf;
  147. /* Try to allocate a buffer, stop for now if out of memory */
  148. iobuf = alloc_iob ( RX_BUF_SIZE );
  149. if ( ! iobuf )
  150. break;
  151. /* Keep track of iobuf so close() can free it */
  152. list_add ( &iobuf->list, &virtnet->rx_iobufs );
  153. /* Mark packet length until we know the actual size */
  154. iob_put ( iobuf, RX_BUF_SIZE );
  155. virtnet_enqueue_iob ( netdev, RX_INDEX, iobuf );
  156. virtnet->rx_num_iobufs++;
  157. }
  158. }
  159. /** Helper to free all virtqueue memory
  160. *
  161. * @v netdev Network device
  162. */
  163. static void virtnet_free_virtqueues ( struct net_device *netdev ) {
  164. struct virtnet_nic *virtnet = netdev->priv;
  165. int i;
  166. for ( i = 0; i < QUEUE_NB; i++ ) {
  167. virtio_pci_unmap_capability ( &virtnet->virtqueue[i].notification );
  168. vp_free_vq ( &virtnet->virtqueue[i] );
  169. }
  170. free ( virtnet->virtqueue );
  171. virtnet->virtqueue = NULL;
  172. }
  173. /** Open network device, legacy virtio 0.9.5
  174. *
  175. * @v netdev Network device
  176. * @ret rc Return status code
  177. */
  178. static int virtnet_open_legacy ( struct net_device *netdev ) {
  179. struct virtnet_nic *virtnet = netdev->priv;
  180. unsigned long ioaddr = virtnet->ioaddr;
  181. u32 features;
  182. int i;
  183. /* Reset for sanity */
  184. vp_reset ( ioaddr );
  185. /* Allocate virtqueues */
  186. virtnet->virtqueue = zalloc ( QUEUE_NB *
  187. sizeof ( *virtnet->virtqueue ) );
  188. if ( ! virtnet->virtqueue )
  189. return -ENOMEM;
  190. /* Initialize rx/tx virtqueues */
  191. for ( i = 0; i < QUEUE_NB; i++ ) {
  192. if ( vp_find_vq ( ioaddr, i, &virtnet->virtqueue[i] ) == -1 ) {
  193. DBGC ( virtnet, "VIRTIO-NET %p cannot register queue %d\n",
  194. virtnet, i );
  195. virtnet_free_virtqueues ( netdev );
  196. return -ENOENT;
  197. }
  198. }
  199. /* Initialize rx packets */
  200. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  201. virtnet->rx_num_iobufs = 0;
  202. virtnet_refill_rx_virtqueue ( netdev );
  203. /* Disable interrupts before starting */
  204. netdev_irq ( netdev, 0 );
  205. /* Driver is ready */
  206. features = vp_get_features ( ioaddr );
  207. vp_set_features ( ioaddr, features & ( 1 << VIRTIO_NET_F_MAC ) );
  208. vp_set_status ( ioaddr, VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_DRIVER_OK );
  209. return 0;
  210. }
  211. /** Open network device, modern virtio 1.0
  212. *
  213. * @v netdev Network device
  214. * @ret rc Return status code
  215. */
  216. static int virtnet_open_modern ( struct net_device *netdev ) {
  217. struct virtnet_nic *virtnet = netdev->priv;
  218. u64 features;
  219. u8 status;
  220. /* Negotiate features */
  221. features = vpm_get_features ( &virtnet->vdev );
  222. if ( ! ( features & VIRTIO_F_VERSION_1 ) ) {
  223. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  224. return -EINVAL;
  225. }
  226. vpm_set_features ( &virtnet->vdev, features & (
  227. ( 1ULL << VIRTIO_NET_F_MAC ) |
  228. ( 1ULL << VIRTIO_F_VERSION_1 ) |
  229. ( 1ULL << VIRTIO_F_ANY_LAYOUT ) ) );
  230. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FEATURES_OK );
  231. status = vpm_get_status ( &virtnet->vdev );
  232. if ( ! ( status & VIRTIO_CONFIG_S_FEATURES_OK ) ) {
  233. DBGC ( virtnet, "VIRTIO-NET %p device didn't accept features\n",
  234. virtnet );
  235. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  236. return -EINVAL;
  237. }
  238. /* Allocate virtqueues */
  239. virtnet->virtqueue = zalloc ( QUEUE_NB *
  240. sizeof ( *virtnet->virtqueue ) );
  241. if ( ! virtnet->virtqueue ) {
  242. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  243. return -ENOMEM;
  244. }
  245. /* Initialize rx/tx virtqueues */
  246. if ( vpm_find_vqs ( &virtnet->vdev, QUEUE_NB, virtnet->virtqueue ) ) {
  247. DBGC ( virtnet, "VIRTIO-NET %p cannot register queues\n",
  248. virtnet );
  249. virtnet_free_virtqueues ( netdev );
  250. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_FAILED );
  251. return -ENOENT;
  252. }
  253. /* Disable interrupts before starting */
  254. netdev_irq ( netdev, 0 );
  255. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_DRIVER_OK );
  256. /* Initialize rx packets */
  257. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  258. virtnet->rx_num_iobufs = 0;
  259. virtnet_refill_rx_virtqueue ( netdev );
  260. return 0;
  261. }
  262. /** Open network device
  263. *
  264. * @v netdev Network device
  265. * @ret rc Return status code
  266. */
  267. static int virtnet_open ( struct net_device *netdev ) {
  268. struct virtnet_nic *virtnet = netdev->priv;
  269. if ( virtnet->virtio_version ) {
  270. return virtnet_open_modern ( netdev );
  271. } else {
  272. return virtnet_open_legacy ( netdev );
  273. }
  274. }
  275. /** Close network device
  276. *
  277. * @v netdev Network device
  278. */
  279. static void virtnet_close ( struct net_device *netdev ) {
  280. struct virtnet_nic *virtnet = netdev->priv;
  281. struct io_buffer *iobuf;
  282. struct io_buffer *next_iobuf;
  283. if ( virtnet->virtio_version ) {
  284. vpm_reset ( &virtnet->vdev );
  285. } else {
  286. vp_reset ( virtnet->ioaddr );
  287. }
  288. /* Virtqueues can be freed now that NIC is reset */
  289. virtnet_free_virtqueues ( netdev );
  290. /* Free rx iobufs */
  291. list_for_each_entry_safe ( iobuf, next_iobuf, &virtnet->rx_iobufs, list ) {
  292. free_iob ( iobuf );
  293. }
  294. INIT_LIST_HEAD ( &virtnet->rx_iobufs );
  295. virtnet->rx_num_iobufs = 0;
  296. }
  297. /** Transmit packet
  298. *
  299. * @v netdev Network device
  300. * @v iobuf I/O buffer
  301. * @ret rc Return status code
  302. */
  303. static int virtnet_transmit ( struct net_device *netdev,
  304. struct io_buffer *iobuf ) {
  305. virtnet_enqueue_iob ( netdev, TX_INDEX, iobuf );
  306. return 0;
  307. }
  308. /** Complete packet transmission
  309. *
  310. * @v netdev Network device
  311. */
  312. static void virtnet_process_tx_packets ( struct net_device *netdev ) {
  313. struct virtnet_nic *virtnet = netdev->priv;
  314. struct vring_virtqueue *tx_vq = &virtnet->virtqueue[TX_INDEX];
  315. while ( vring_more_used ( tx_vq ) ) {
  316. struct io_buffer *iobuf = vring_get_buf ( tx_vq, NULL );
  317. DBGC2 ( virtnet, "VIRTIO-NET %p tx complete iobuf %p\n",
  318. virtnet, iobuf );
  319. netdev_tx_complete ( netdev, iobuf );
  320. }
  321. }
  322. /** Complete packet reception
  323. *
  324. * @v netdev Network device
  325. */
  326. static void virtnet_process_rx_packets ( struct net_device *netdev ) {
  327. struct virtnet_nic *virtnet = netdev->priv;
  328. struct vring_virtqueue *rx_vq = &virtnet->virtqueue[RX_INDEX];
  329. while ( vring_more_used ( rx_vq ) ) {
  330. unsigned int len;
  331. struct io_buffer *iobuf = vring_get_buf ( rx_vq, &len );
  332. /* Release ownership of iobuf */
  333. list_del ( &iobuf->list );
  334. virtnet->rx_num_iobufs--;
  335. /* Update iobuf length */
  336. iob_unput ( iobuf, RX_BUF_SIZE );
  337. iob_put ( iobuf, len - sizeof ( struct virtio_net_hdr ) );
  338. DBGC2 ( virtnet, "VIRTIO-NET %p rx complete iobuf %p len %zd\n",
  339. virtnet, iobuf, iob_len ( iobuf ) );
  340. /* Pass completed packet to the network stack */
  341. netdev_rx ( netdev, iobuf );
  342. }
  343. virtnet_refill_rx_virtqueue ( netdev );
  344. }
  345. /** Poll for completed and received packets
  346. *
  347. * @v netdev Network device
  348. */
  349. static void virtnet_poll ( struct net_device *netdev ) {
  350. struct virtnet_nic *virtnet = netdev->priv;
  351. /* Acknowledge interrupt. This is necessary for UNDI operation and
  352. * interrupts that are raised despite VRING_AVAIL_F_NO_INTERRUPT being
  353. * set (that flag is just a hint and the hypervisor does not have to
  354. * honor it).
  355. */
  356. if ( virtnet->virtio_version ) {
  357. vpm_get_isr ( &virtnet->vdev );
  358. } else {
  359. vp_get_isr ( virtnet->ioaddr );
  360. }
  361. virtnet_process_tx_packets ( netdev );
  362. virtnet_process_rx_packets ( netdev );
  363. }
  364. /** Enable or disable interrupts
  365. *
  366. * @v netdev Network device
  367. * @v enable Interrupts should be enabled
  368. */
  369. static void virtnet_irq ( struct net_device *netdev, int enable ) {
  370. struct virtnet_nic *virtnet = netdev->priv;
  371. int i;
  372. for ( i = 0; i < QUEUE_NB; i++ ) {
  373. if ( enable )
  374. vring_enable_cb ( &virtnet->virtqueue[i] );
  375. else
  376. vring_disable_cb ( &virtnet->virtqueue[i] );
  377. }
  378. }
  379. /** virtio-net device operations */
  380. static struct net_device_operations virtnet_operations = {
  381. .open = virtnet_open,
  382. .close = virtnet_close,
  383. .transmit = virtnet_transmit,
  384. .poll = virtnet_poll,
  385. .irq = virtnet_irq,
  386. };
  387. /**
  388. * Probe PCI device, legacy virtio 0.9.5
  389. *
  390. * @v pci PCI device
  391. * @ret rc Return status code
  392. */
  393. static int virtnet_probe_legacy ( struct pci_device *pci ) {
  394. unsigned long ioaddr = pci->ioaddr;
  395. struct net_device *netdev;
  396. struct virtnet_nic *virtnet;
  397. u32 features;
  398. int rc;
  399. /* Allocate and hook up net device */
  400. netdev = alloc_etherdev ( sizeof ( *virtnet ) );
  401. if ( ! netdev )
  402. return -ENOMEM;
  403. netdev_init ( netdev, &virtnet_operations );
  404. virtnet = netdev->priv;
  405. virtnet->ioaddr = ioaddr;
  406. pci_set_drvdata ( pci, netdev );
  407. netdev->dev = &pci->dev;
  408. DBGC ( virtnet, "VIRTIO-NET %p busaddr=%s ioaddr=%#lx irq=%d\n",
  409. virtnet, pci->dev.name, ioaddr, pci->irq );
  410. /* Enable PCI bus master and reset NIC */
  411. adjust_pci_device ( pci );
  412. vp_reset ( ioaddr );
  413. /* Load MAC address */
  414. features = vp_get_features ( ioaddr );
  415. if ( features & ( 1 << VIRTIO_NET_F_MAC ) ) {
  416. vp_get ( ioaddr, offsetof ( struct virtio_net_config, mac ),
  417. netdev->hw_addr, ETH_ALEN );
  418. DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
  419. eth_ntoa ( netdev->hw_addr ) );
  420. }
  421. /* Register network device */
  422. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  423. goto err_register_netdev;
  424. /* Mark link as up, control virtqueue is not used */
  425. netdev_link_up ( netdev );
  426. return 0;
  427. unregister_netdev ( netdev );
  428. err_register_netdev:
  429. vp_reset ( ioaddr );
  430. netdev_nullify ( netdev );
  431. netdev_put ( netdev );
  432. return rc;
  433. }
  434. /**
  435. * Probe PCI device, modern virtio 1.0
  436. *
  437. * @v pci PCI device
  438. * @v found_dev Set to non-zero if modern device was found (probe may still fail)
  439. * @ret rc Return status code
  440. */
  441. static int virtnet_probe_modern ( struct pci_device *pci, int *found_dev ) {
  442. struct net_device *netdev;
  443. struct virtnet_nic *virtnet;
  444. u64 features;
  445. int rc, common, isr, notify, config, device;
  446. common = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_COMMON_CFG );
  447. if ( ! common ) {
  448. DBG ( "Common virtio capability not found!\n" );
  449. return -ENODEV;
  450. }
  451. *found_dev = 1;
  452. isr = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_ISR_CFG );
  453. notify = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_NOTIFY_CFG );
  454. config = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_PCI_CFG );
  455. if ( ! isr || ! notify || ! config ) {
  456. DBG ( "Missing virtio capabilities %i/%i/%i/%i\n",
  457. common, isr, notify, config );
  458. return -EINVAL;
  459. }
  460. device = virtio_pci_find_capability ( pci, VIRTIO_PCI_CAP_DEVICE_CFG );
  461. /* Allocate and hook up net device */
  462. netdev = alloc_etherdev ( sizeof ( *virtnet ) );
  463. if ( ! netdev )
  464. return -ENOMEM;
  465. netdev_init ( netdev, &virtnet_operations );
  466. virtnet = netdev->priv;
  467. pci_set_drvdata ( pci, netdev );
  468. netdev->dev = &pci->dev;
  469. DBGC ( virtnet, "VIRTIO-NET modern %p busaddr=%s irq=%d\n",
  470. virtnet, pci->dev.name, pci->irq );
  471. virtnet->vdev.pci = pci;
  472. rc = virtio_pci_map_capability ( pci, common,
  473. sizeof ( struct virtio_pci_common_cfg ), 4,
  474. 0, sizeof ( struct virtio_pci_common_cfg ),
  475. &virtnet->vdev.common );
  476. if ( rc )
  477. goto err_map_common;
  478. rc = virtio_pci_map_capability ( pci, isr, sizeof ( u8 ), 1,
  479. 0, 1,
  480. &virtnet->vdev.isr );
  481. if ( rc )
  482. goto err_map_isr;
  483. virtnet->vdev.notify_cap_pos = notify;
  484. virtnet->vdev.cfg_cap_pos = config;
  485. /* Map the device capability */
  486. if ( device ) {
  487. rc = virtio_pci_map_capability ( pci, device,
  488. 0, 4, 0, sizeof ( struct virtio_net_config ),
  489. &virtnet->vdev.device );
  490. if ( rc )
  491. goto err_map_device;
  492. }
  493. /* Enable the PCI device */
  494. adjust_pci_device ( pci );
  495. /* Reset the device and set initial status bits */
  496. vpm_reset ( &virtnet->vdev );
  497. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_ACKNOWLEDGE );
  498. vpm_add_status ( &virtnet->vdev, VIRTIO_CONFIG_S_DRIVER );
  499. /* Load MAC address */
  500. if ( device ) {
  501. features = vpm_get_features ( &virtnet->vdev );
  502. if ( features & ( 1ULL << VIRTIO_NET_F_MAC ) ) {
  503. vpm_get ( &virtnet->vdev,
  504. offsetof ( struct virtio_net_config, mac ),
  505. netdev->hw_addr, ETH_ALEN );
  506. DBGC ( virtnet, "VIRTIO-NET %p mac=%s\n", virtnet,
  507. eth_ntoa ( netdev->hw_addr ) );
  508. }
  509. }
  510. /* We need a valid MAC address */
  511. if ( ! is_valid_ether_addr ( netdev->hw_addr ) ) {
  512. rc = -EADDRNOTAVAIL;
  513. goto err_mac_address;
  514. }
  515. /* Register network device */
  516. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  517. goto err_register_netdev;
  518. /* Mark link as up, control virtqueue is not used */
  519. netdev_link_up ( netdev );
  520. virtnet->virtio_version = 1;
  521. return 0;
  522. unregister_netdev ( netdev );
  523. err_register_netdev:
  524. err_mac_address:
  525. vpm_reset ( &virtnet->vdev );
  526. netdev_nullify ( netdev );
  527. netdev_put ( netdev );
  528. virtio_pci_unmap_capability ( &virtnet->vdev.device );
  529. err_map_device:
  530. virtio_pci_unmap_capability ( &virtnet->vdev.isr );
  531. err_map_isr:
  532. virtio_pci_unmap_capability ( &virtnet->vdev.common );
  533. err_map_common:
  534. return rc;
  535. }
  536. /**
  537. * Probe PCI device
  538. *
  539. * @v pci PCI device
  540. * @ret rc Return status code
  541. */
  542. static int virtnet_probe ( struct pci_device *pci ) {
  543. int found_modern = 0;
  544. int rc = virtnet_probe_modern ( pci, &found_modern );
  545. if ( ! found_modern && pci->device < 0x1040 ) {
  546. /* fall back to the legacy probe */
  547. rc = virtnet_probe_legacy ( pci );
  548. }
  549. return rc;
  550. }
  551. /**
  552. * Remove device
  553. *
  554. * @v pci PCI device
  555. */
  556. static void virtnet_remove ( struct pci_device *pci ) {
  557. struct net_device *netdev = pci_get_drvdata ( pci );
  558. struct virtnet_nic *virtnet = netdev->priv;
  559. virtio_pci_unmap_capability ( &virtnet->vdev.device );
  560. virtio_pci_unmap_capability ( &virtnet->vdev.isr );
  561. virtio_pci_unmap_capability ( &virtnet->vdev.common );
  562. unregister_netdev ( netdev );
  563. netdev_nullify ( netdev );
  564. netdev_put ( netdev );
  565. }
  566. static struct pci_device_id virtnet_nics[] = {
  567. PCI_ROM(0x1af4, 0x1000, "virtio-net", "Virtio Network Interface", 0),
  568. PCI_ROM(0x1af4, 0x1041, "virtio-net", "Virtio Network Interface 1.0", 0),
  569. };
  570. struct pci_driver virtnet_driver __pci_driver = {
  571. .ids = virtnet_nics,
  572. .id_count = ( sizeof ( virtnet_nics ) / sizeof ( virtnet_nics[0] ) ),
  573. .probe = virtnet_probe,
  574. .remove = virtnet_remove,
  575. };