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.

legacy.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #include <stdint.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <ipxe/if_ether.h>
  5. #include <ipxe/netdevice.h>
  6. #include <ipxe/ethernet.h>
  7. #include <ipxe/iobuf.h>
  8. #include <nic.h>
  9. /*
  10. * Quick and dirty compatibility layer
  11. *
  12. * This should allow old-API PCI drivers to at least function until
  13. * they are updated. It will not help non-PCI drivers.
  14. *
  15. * No drivers should rely on this code. It will be removed asap.
  16. *
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  19. struct nic nic;
  20. static int legacy_registered = 0;
  21. static int legacy_transmit ( struct net_device *netdev, struct io_buffer *iobuf ) {
  22. struct nic *nic = netdev->priv;
  23. struct ethhdr *ethhdr;
  24. DBG ( "Transmitting %zd bytes\n", iob_len ( iobuf ) );
  25. iob_pad ( iobuf, ETH_ZLEN );
  26. ethhdr = iobuf->data;
  27. iob_pull ( iobuf, sizeof ( *ethhdr ) );
  28. nic->nic_op->transmit ( nic, ( const char * ) ethhdr->h_dest,
  29. ntohs ( ethhdr->h_protocol ),
  30. iob_len ( iobuf ), iobuf->data );
  31. netdev_tx_complete ( netdev, iobuf );
  32. return 0;
  33. }
  34. static void legacy_poll ( struct net_device *netdev ) {
  35. struct nic *nic = netdev->priv;
  36. struct io_buffer *iobuf;
  37. iobuf = alloc_iob ( ETH_FRAME_LEN );
  38. if ( ! iobuf )
  39. return;
  40. nic->packet = iobuf->data;
  41. if ( nic->nic_op->poll ( nic, 1 ) ) {
  42. DBG ( "Received %d bytes\n", nic->packetlen );
  43. iob_put ( iobuf, nic->packetlen );
  44. netdev_rx ( netdev, iobuf );
  45. } else {
  46. free_iob ( iobuf );
  47. }
  48. }
  49. static int legacy_open ( struct net_device *netdev __unused ) {
  50. /* Nothing to do */
  51. return 0;
  52. }
  53. static void legacy_close ( struct net_device *netdev __unused ) {
  54. /* Nothing to do */
  55. }
  56. static void legacy_irq ( struct net_device *netdev __unused, int enable ) {
  57. struct nic *nic = netdev->priv;
  58. nic->nic_op->irq ( nic, ( enable ? ENABLE : DISABLE ) );
  59. }
  60. static struct net_device_operations legacy_operations = {
  61. .open = legacy_open,
  62. .close = legacy_close,
  63. .transmit = legacy_transmit,
  64. .poll = legacy_poll,
  65. .irq = legacy_irq,
  66. };
  67. int legacy_probe ( void *hwdev,
  68. void ( * set_drvdata ) ( void *hwdev, void *priv ),
  69. struct device *dev,
  70. int ( * probe ) ( struct nic *nic, void *hwdev ),
  71. void ( * disable ) ( struct nic *nic, void *hwdev ) ) {
  72. struct net_device *netdev;
  73. int rc;
  74. if ( legacy_registered )
  75. return -EBUSY;
  76. netdev = alloc_etherdev ( 0 );
  77. if ( ! netdev )
  78. return -ENOMEM;
  79. netdev_init ( netdev, &legacy_operations );
  80. netdev->priv = &nic;
  81. memset ( &nic, 0, sizeof ( nic ) );
  82. set_drvdata ( hwdev, netdev );
  83. netdev->dev = dev;
  84. nic.node_addr = netdev->hw_addr;
  85. nic.irqno = dev->desc.irq;
  86. if ( ! probe ( &nic, hwdev ) ) {
  87. rc = -ENODEV;
  88. goto err_probe;
  89. }
  90. /* Overwrite the IRQ number. Some legacy devices set
  91. * nic->irqno to 0 in the probe routine to indicate that they
  92. * don't support interrupts; doing this allows the timer
  93. * interrupt to be used instead.
  94. */
  95. dev->desc.irq = nic.irqno;
  96. if ( ( rc = register_netdev ( netdev ) ) != 0 )
  97. goto err_register;
  98. /* Mark as link up; legacy devices don't handle link state */
  99. netdev_link_up ( netdev );
  100. /* Do not remove this message */
  101. printf ( "WARNING: Using legacy NIC wrapper on %s\n",
  102. netdev->ll_protocol->ntoa ( nic.node_addr ) );
  103. legacy_registered = 1;
  104. return 0;
  105. err_register:
  106. disable ( &nic, hwdev );
  107. err_probe:
  108. netdev_nullify ( netdev );
  109. netdev_put ( netdev );
  110. return rc;
  111. }
  112. void legacy_remove ( void *hwdev,
  113. void * ( * get_drvdata ) ( void *hwdev ),
  114. void ( * disable ) ( struct nic *nic, void *hwdev ) ) {
  115. struct net_device *netdev = get_drvdata ( hwdev );
  116. struct nic *nic = netdev->priv;
  117. unregister_netdev ( netdev );
  118. disable ( nic, hwdev );
  119. netdev_nullify ( netdev );
  120. netdev_put ( netdev );
  121. legacy_registered = 0;
  122. }
  123. int dummy_connect ( struct nic *nic __unused ) {
  124. return 1;
  125. }
  126. void dummy_irq ( struct nic *nic __unused, irq_action_t irq_action __unused ) {
  127. return;
  128. }