netdev.c 1.9 KB

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