netdev.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* NIC driver registry: see netdev.h. */
  2. #include "netdev.h"
  3. #include "asix.h"
  4. #include "e1000e.h"
  5. #include "io.h"
  6. #include "ne2000.h"
  7. #include "net.h"
  8. #include "process.h"
  9. #include "r8168.h"
  10. static const struct netdev *const netdev_drivers[] = {
  11. /* e1000e is the active test NIC; the ASIX is kept as the management
  12. * link by netdev_mgmt() even while e1000e is active. */
  13. &e1000e_netdev,
  14. &r8168_netdev,
  15. &ne2000_netdev,
  16. &asix_netdev,
  17. };
  18. static const struct netdev *active;
  19. static int probed;
  20. static unsigned int busy;
  21. static unsigned int owner_pid; /* process that took the NIC (busy > 0) */
  22. /* Shared receive scratch: see netdev.h. One buffer, reused by every protocol
  23. * that polls frames (they never run at the same time: the NIC is owned by one
  24. * command via netdev_acquire()). */
  25. static unsigned char rx_scratch[NETDEV_FRAME_MAX];
  26. unsigned char *netdev_rx_scratch(void)
  27. {
  28. return rx_scratch;
  29. }
  30. void netdev_acquire(void)
  31. {
  32. if (busy == 0 && current_proc())
  33. owner_pid = current_proc()->pid;
  34. busy++;
  35. }
  36. void netdev_release(void)
  37. {
  38. if (busy && --busy == 0)
  39. owner_pid = 0;
  40. }
  41. /* The process currently owning the NIC (0 when free), and a hard reset for
  42. * the reaper: a process killed while inside a net operation must not leave
  43. * the receiver owned forever (it would pause the kexec watcher). */
  44. unsigned int netdev_owner(void)
  45. {
  46. return owner_pid;
  47. }
  48. void netdev_release_all(void)
  49. {
  50. busy = 0;
  51. owner_pid = 0;
  52. }
  53. int netdev_in_use(void)
  54. {
  55. return busy != 0;
  56. }
  57. /* Apply the driver's link-local/static default when it becomes active.
  58. * The ASIX direct link uses 192.168.50.2/24; the e1000e keeps the router
  59. * subnet at 192.168.178.240. */
  60. static void netdev_apply_defaults(const struct netdev *nd)
  61. {
  62. if (nd && nd->default_ip)
  63. net_configure(nd->default_ip, nd->default_mask, 0);
  64. }
  65. const struct netdev *netdev_active(void)
  66. {
  67. unsigned int i;
  68. if (!probed) {
  69. probed = 1;
  70. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]); i++) {
  71. if (netdev_drivers[i]->init() == 0) {
  72. active = netdev_drivers[i];
  73. netdev_apply_defaults(active);
  74. break;
  75. }
  76. }
  77. }
  78. return active;
  79. }
  80. /* The ASIX dongle is the direct management link when present. Keeping
  81. * netlog/netkexec here lets the active driver be switched to e1000e for
  82. * testing without losing the netmon console or `make net-dev`. */
  83. const struct netdev *netdev_mgmt(void)
  84. {
  85. if (asix_netdev.init() == 0)
  86. return &asix_netdev;
  87. return netdev_active();
  88. }
  89. /* Stop the NICs before kexec overwrites the kernel. A still-running
  90. * receive ring can DMA into pages the new kernel has already reused. */
  91. void netdev_shutdown(void)
  92. {
  93. const struct netdev *mgmt = netdev_mgmt();
  94. const struct netdev *act = netdev_active();
  95. if (mgmt && mgmt->stop)
  96. mgmt->stop();
  97. if (act && act != mgmt && act->stop)
  98. act->stop();
  99. }
  100. int netdev_get_mac(unsigned char mac[6])
  101. {
  102. const struct netdev *nd = netdev_active();
  103. unsigned int i;
  104. if (!nd) {
  105. for (i = 0; i < 6; i++)
  106. mac[i] = 0;
  107. return -1;
  108. }
  109. nd->get_mac(mac);
  110. return 0;
  111. }
  112. static int name_eq(const char *a, const char *b)
  113. {
  114. while (*a && *b) {
  115. if (*a != *b)
  116. return 0;
  117. a++;
  118. b++;
  119. }
  120. return *a == 0 && *b == 0;
  121. }
  122. int netdev_select(const char *name)
  123. {
  124. unsigned int i;
  125. if (!name || !*name || busy)
  126. return -1;
  127. while (*name == ' ')
  128. name++;
  129. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  130. i++) {
  131. if (!name_eq(netdev_drivers[i]->name, name))
  132. continue;
  133. if (active == netdev_drivers[i]) {
  134. probed = 1;
  135. return 0;
  136. }
  137. if (netdev_drivers[i]->init() != 0)
  138. return -1;
  139. active = netdev_drivers[i];
  140. probed = 1;
  141. netdev_apply_defaults(active);
  142. return 0;
  143. }
  144. return -1;
  145. }
  146. static void print_netdev(const char *label, const struct netdev *nd)
  147. {
  148. io_putstr("netdev: ");
  149. io_putstr(label);
  150. io_putstr(nd->name);
  151. io_putstr(" (");
  152. io_putstr(nd->chip);
  153. io_putstr(")\n");
  154. }
  155. void program_netdev_main(const char *arg)
  156. {
  157. const struct netdev *mgmt;
  158. if (arg && *arg) {
  159. while (*arg == ' ')
  160. arg++;
  161. if (netdev_select(arg) != 0) {
  162. io_putstr("netdev: cannot select '");
  163. io_putstr(arg);
  164. io_putstr("'\n");
  165. return;
  166. }
  167. } else if (!netdev_active()) {
  168. io_putstr("netdev: no NIC\n");
  169. return;
  170. }
  171. print_netdev("active ", netdev_active());
  172. if (netdev_active()->print_info)
  173. netdev_active()->print_info();
  174. mgmt = netdev_mgmt();
  175. if (mgmt && mgmt != netdev_active())
  176. print_netdev("mgmt ", mgmt);
  177. if (!(arg && *arg))
  178. io_putstr("netdev: use netdev asix|e1000e|r8168|ne2000\n");
  179. }