netdev.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* Called from the ASIX device probe after a successful hotplug
  90. * enumeration. The boot-time probe caches a NULL result, so without this a
  91. * dongle plugged in later would only be usable through `netdev asix`. */
  92. void netdev_hotplug(void)
  93. {
  94. if (active || !probed)
  95. return;
  96. if (asix_netdev.init() == 0) {
  97. active = &asix_netdev;
  98. netdev_apply_defaults(active);
  99. }
  100. }
  101. /* Stop the NICs before kexec overwrites the kernel. A still-running
  102. * receive ring can DMA into pages the new kernel has already reused. */
  103. void netdev_shutdown(void)
  104. {
  105. const struct netdev *mgmt = netdev_mgmt();
  106. const struct netdev *act = netdev_active();
  107. if (mgmt && mgmt->stop)
  108. mgmt->stop();
  109. if (act && act != mgmt && act->stop)
  110. act->stop();
  111. }
  112. int netdev_get_mac(unsigned char mac[6])
  113. {
  114. const struct netdev *nd = netdev_active();
  115. unsigned int i;
  116. if (!nd) {
  117. for (i = 0; i < 6; i++)
  118. mac[i] = 0;
  119. return -1;
  120. }
  121. nd->get_mac(mac);
  122. return 0;
  123. }
  124. static int name_eq(const char *a, const char *b)
  125. {
  126. while (*a && *b) {
  127. if (*a != *b)
  128. return 0;
  129. a++;
  130. b++;
  131. }
  132. return *a == 0 && *b == 0;
  133. }
  134. int netdev_select(const char *name)
  135. {
  136. unsigned int i;
  137. if (!name || !*name || busy)
  138. return -1;
  139. while (*name == ' ')
  140. name++;
  141. for (i = 0; i < sizeof(netdev_drivers) / sizeof(netdev_drivers[0]);
  142. i++) {
  143. if (!name_eq(netdev_drivers[i]->name, name))
  144. continue;
  145. if (active == netdev_drivers[i]) {
  146. probed = 1;
  147. return 0;
  148. }
  149. if (netdev_drivers[i]->init() != 0)
  150. return -1;
  151. active = netdev_drivers[i];
  152. probed = 1;
  153. netdev_apply_defaults(active);
  154. return 0;
  155. }
  156. return -1;
  157. }
  158. static void print_netdev(const char *label, const struct netdev *nd)
  159. {
  160. io_putstr("netdev: ");
  161. io_putstr(label);
  162. io_putstr(nd->name);
  163. io_putstr(" (");
  164. io_putstr(nd->chip);
  165. io_putstr(")\n");
  166. }
  167. void program_netdev_main(const char *arg)
  168. {
  169. const struct netdev *mgmt;
  170. if (arg && *arg) {
  171. while (*arg == ' ')
  172. arg++;
  173. if (netdev_select(arg) != 0) {
  174. io_putstr("netdev: cannot select '");
  175. io_putstr(arg);
  176. io_putstr("'\n");
  177. return;
  178. }
  179. } else if (!netdev_active()) {
  180. io_putstr("netdev: no NIC\n");
  181. return;
  182. }
  183. print_netdev("active ", netdev_active());
  184. if (netdev_active()->print_info)
  185. netdev_active()->print_info();
  186. mgmt = netdev_mgmt();
  187. if (mgmt && mgmt != netdev_active())
  188. print_netdev("mgmt ", mgmt);
  189. if (!(arg && *arg))
  190. io_putstr("netdev: use netdev asix|e1000e|r8168|ne2000\n");
  191. }