netdev.c 1.8 KB

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