netdev.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /* NIC driver registry: see netdev.h. */
  2. #include "netdev.h"
  3. #include "asix.h"
  4. #include "e1000e.h"
  5. #include "ne2000.h"
  6. #include "process.h"
  7. #include "r8168.h"
  8. static const struct netdev *const netdev_drivers[] = {
  9. /* Intel PCIe/PCH first: the X201's 82577LM is an e1000e. On the
  10. * AT3IONT-I this probe misses and r8168 drives the real NIC. */
  11. &e1000e_netdev,
  12. &r8168_netdev,
  13. &ne2000_netdev,
  14. &asix_netdev,
  15. };
  16. static const struct netdev *active;
  17. static int probed;
  18. static unsigned int busy;
  19. static unsigned int owner_pid; /* process that took the NIC (busy > 0) */
  20. /* Shared receive scratch: see netdev.h. One buffer, reused by every protocol
  21. * that polls frames (they never run at the same time: the NIC is owned by one
  22. * command via netdev_acquire()). */
  23. static unsigned char rx_scratch[NETDEV_FRAME_MAX];
  24. unsigned char *netdev_rx_scratch(void)
  25. {
  26. return rx_scratch;
  27. }
  28. void netdev_acquire(void)
  29. {
  30. if (busy == 0 && current_proc())
  31. owner_pid = current_proc()->pid;
  32. busy++;
  33. }
  34. void netdev_release(void)
  35. {
  36. if (busy && --busy == 0)
  37. owner_pid = 0;
  38. }
  39. /* The process currently owning the NIC (0 when free), and a hard reset for
  40. * the reaper: a process killed while inside a net operation must not leave
  41. * the receiver owned forever (it would pause the kexec watcher). */
  42. unsigned int netdev_owner(void)
  43. {
  44. return owner_pid;
  45. }
  46. void netdev_release_all(void)
  47. {
  48. busy = 0;
  49. owner_pid = 0;
  50. }
  51. int netdev_in_use(void)
  52. {
  53. return busy != 0;
  54. }
  55. const struct netdev *netdev_active(void)
  56. {
  57. unsigned int i;
  58. if (!probed) {
  59. probed = 1;
  60. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  61. if (netdev_drivers[i]->init() == 0) {
  62. active = netdev_drivers[i];
  63. break;
  64. }
  65. }
  66. }
  67. return active;
  68. }
  69. int netdev_get_mac(unsigned char mac[6])
  70. {
  71. const struct netdev *nd = netdev_active();
  72. unsigned int i;
  73. if (!nd) {
  74. for (i = 0; i < 6; i++)
  75. mac[i] = 0;
  76. return -1;
  77. }
  78. nd->get_mac(mac);
  79. return 0;
  80. }