Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

virtio-net.c 18KB

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