NetworkAdapter.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  2. * Copyright (c) 2021, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MACAddress.h>
  7. #include <Kernel/Arch/x86/IO.h>
  8. #include <Kernel/Bus/PCI/API.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Net/NE2000/NetworkAdapter.h>
  11. #include <Kernel/Net/NetworkingManagement.h>
  12. #include <Kernel/Sections.h>
  13. namespace Kernel {
  14. /**
  15. * The NE2000 is an ancient 10 Mib/s Ethernet network card standard by Novell
  16. * from the late 80s. Based on National Semiconductor's DP8390 Ethernet chip
  17. * or compatible, they were known to be extremely bare-bones but also very
  18. * cheap entry-level cards.
  19. *
  20. * QEMU supports them with the ne2k_{isa,pci} devices, physical incarnations
  21. * were available from different manufacturers for the ISA bus and later on
  22. * the PCI bus, including:
  23. * - Realtek's RTL8029
  24. * - VIA Technologies, Inc.'s VT86C926
  25. *
  26. * Official documentation from National Semiconductor includes:
  27. * - Datasheet "DP8390D/NS32490D NIC Network Interface Controller"
  28. * - Application Note 874 "Writing Drivers for the DP8390 NIC Family of Ethernet Controllers"
  29. *
  30. * This driver supports only the PCI variant.
  31. *
  32. * Remember, friends don't let friends use NE2000 network cards :^)
  33. */
  34. // Page 0 registers
  35. static constexpr u8 REG_RW_COMMAND = 0x00;
  36. static constexpr u8 BIT_COMMAND_STOP = (0b1 << 0);
  37. static constexpr u8 BIT_COMMAND_START = (0b1 << 1);
  38. static constexpr u8 BIT_COMMAND_TXP = (0b1 << 2);
  39. static constexpr u8 BIT_COMMAND_DMA_READ = (0b001 << 3);
  40. static constexpr u8 BIT_COMMAND_DMA_WRITE = (0b010 << 3);
  41. static constexpr u8 BIT_COMMAND_DMA_SEND = (0b011 << 3);
  42. static constexpr u8 BIT_COMMAND_DMA_ABORT = (0b100 << 3);
  43. static constexpr u8 BIT_COMMAND_DMA_FIELD = (0b111 << 3);
  44. static constexpr u8 BIT_COMMAND_PAGE1 = (0b01 << 6);
  45. static constexpr u8 BIT_COMMAND_PAGE2 = (0b10 << 6);
  46. static constexpr u8 BIT_COMMAND_PAGE_FIELD = (0b11 << 6);
  47. static constexpr u8 REG_WR_PAGESTART = 0x01;
  48. static constexpr u8 REG_WR_PAGESTOP = 0x02;
  49. static constexpr u8 REG_RW_BOUNDARY = 0x03;
  50. static constexpr u8 REG_RD_TRANSMITSTATUS = 0x04;
  51. static constexpr u8 REG_WR_TRANSMITPAGE = 0x04;
  52. static constexpr u8 REG_RD_NCR = 0x05;
  53. static constexpr u8 REG_WR_TRANSMITBYTECOUNT0 = 0x05;
  54. static constexpr u8 REG_WR_TRANSMITBYTECOUNT1 = 0x06;
  55. static constexpr u8 REG_RW_INTERRUPTSTATUS = 0x07;
  56. static constexpr u8 REG_RD_CRDMA0 = 0x08;
  57. static constexpr u8 REG_WR_REMOTESTARTADDRESS0 = 0x08;
  58. static constexpr u8 REG_RD_CRDMA1 = 0x09;
  59. static constexpr u8 REG_WR_REMOTESTARTADDRESS1 = 0x09;
  60. static constexpr u8 REG_WR_REMOTEBYTECOUNT0 = 0x0a;
  61. static constexpr u8 REG_WR_REMOTEBYTECOUNT1 = 0x0b;
  62. static constexpr u8 REG_RD_RECEIVESTATUS = 0x0c;
  63. static constexpr u8 BIT_RECEIVESTATUS_PRX = (0b1 << 0);
  64. static constexpr u8 BIT_RECEIVESTATUS_CRC = (0b1 << 1);
  65. static constexpr u8 BIT_RECEIVESTATUS_FAE = (0b1 << 2);
  66. static constexpr u8 BIT_RECEIVESTATUS_FO = (0b1 << 3);
  67. static constexpr u8 BIT_RECEIVESTATUS_MPA = (0b1 << 4);
  68. static constexpr u8 REG_WR_RECEIVECONFIGURATION = 0x0c;
  69. static constexpr u8 BIT_RECEIVECONFIGURATION_SEP = (0b1 << 0);
  70. static constexpr u8 BIT_RECEIVECONFIGURATION_AR = (0b1 << 1);
  71. static constexpr u8 BIT_RECEIVECONFIGURATION_AB = (0b1 << 2);
  72. static constexpr u8 BIT_RECEIVECONFIGURATION_AM = (0b1 << 3);
  73. static constexpr u8 BIT_RECEIVECONFIGURATION_PRO = (0b1 << 4);
  74. static constexpr u8 BIT_RECEIVECONFIGURATION_MON = (0b1 << 5);
  75. static constexpr u8 REG_RD_FAE_TALLY = 0x0d;
  76. static constexpr u8 REG_WR_TRANSMITCONFIGURATION = 0x0d;
  77. static constexpr u8 BIT_WR_TRANSMITCONFIGURATION_LOOPBACK = (0b10 << 0);
  78. static constexpr u8 REG_RD_CRC_TALLY = 0x0e;
  79. static constexpr u8 REG_WR_DATACONFIGURATION = 0x0e;
  80. static constexpr u8 BIT_DATACONFIGURATION_WTS = (0b1 << 0);
  81. static constexpr u8 BIT_DATACONFIGURATION_BOS = (0b1 << 1);
  82. static constexpr u8 BIT_DATACONFIGURATION_LS = (0b1 << 2);
  83. static constexpr u8 BIT_DATACONFIGURATION_FIFO_8B = (0b10 << 5);
  84. static constexpr u8 REG_RD_MISS_PKT_TALLY = 0x0f;
  85. static constexpr u8 REG_WR_INTERRUPTMASK = 0x0f;
  86. static constexpr u8 BIT_INTERRUPTMASK_PRX = (0b1 << 0);
  87. static constexpr u8 BIT_INTERRUPTMASK_PTX = (0b1 << 1);
  88. static constexpr u8 BIT_INTERRUPTMASK_RXE = (0b1 << 2);
  89. static constexpr u8 BIT_INTERRUPTMASK_TXE = (0b1 << 3);
  90. static constexpr u8 BIT_INTERRUPTMASK_OVW = (0b1 << 4);
  91. static constexpr u8 BIT_INTERRUPTMASK_CNT = (0b1 << 5);
  92. static constexpr u8 BIT_INTERRUPTMASK_RDC = (0b1 << 6);
  93. static constexpr u8 BIT_INTERRUPTMASK_RST = (0b1 << 7);
  94. static constexpr u8 REG_RW_IOPORT = 0x10;
  95. // Page 1 registers
  96. static constexpr u8 REG_RW_PHYSICALADDRESS0 = 0x01;
  97. static constexpr u8 REG_RW_CURRENT = 0x07;
  98. static constexpr int NE2K_PAGE_SIZE = 256;
  99. static constexpr int NE2K_RAM_BEGIN = 16384;
  100. static constexpr int NE2K_RAM_END = 32768;
  101. static constexpr int NE2K_RAM_SIZE = NE2K_RAM_END - NE2K_RAM_BEGIN;
  102. static constexpr int NE2K_RAM_SEND_BEGIN = 16384;
  103. static constexpr int NE2K_RAM_SEND_END = 16384 + 6 * NE2K_PAGE_SIZE;
  104. static constexpr int NE2K_RAM_SEND_SIZE = NE2K_RAM_SEND_END - NE2K_RAM_SEND_BEGIN;
  105. static constexpr int NE2K_RAM_RECV_BEGIN = NE2K_RAM_SEND_END;
  106. static constexpr int NE2K_RAM_RECV_END = NE2K_RAM_END;
  107. static constexpr int NE2K_RAM_RECV_SIZE = NE2K_RAM_RECV_END - NE2K_RAM_RECV_BEGIN;
  108. static_assert(NE2K_RAM_BEGIN % NE2K_PAGE_SIZE == 0);
  109. static_assert(NE2K_RAM_END % NE2K_PAGE_SIZE == 0);
  110. static_assert(NE2K_RAM_SEND_BEGIN % NE2K_PAGE_SIZE == 0);
  111. static_assert(NE2K_RAM_SEND_END % NE2K_PAGE_SIZE == 0);
  112. static_assert(NE2K_RAM_RECV_BEGIN % NE2K_PAGE_SIZE == 0);
  113. static_assert(NE2K_RAM_RECV_END % NE2K_PAGE_SIZE == 0);
  114. struct [[gnu::packed]] received_packet_header {
  115. u8 status;
  116. u8 next_packet_page;
  117. u16 length;
  118. };
  119. UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
  120. {
  121. constexpr auto ne2k_ids = Array {
  122. PCI::HardwareID { 0x10EC, 0x8029 }, // RealTek RTL-8029(AS)
  123. // List of clones, taken from Linux's ne2k-pci.c
  124. PCI::HardwareID { 0x1050, 0x0940 }, // Winbond 89C940
  125. PCI::HardwareID { 0x11f6, 0x1401 }, // Compex RL2000
  126. PCI::HardwareID { 0x8e2e, 0x3000 }, // KTI ET32P2
  127. PCI::HardwareID { 0x4a14, 0x5000 }, // NetVin NV5000SC
  128. PCI::HardwareID { 0x1106, 0x0926 }, // Via 86C926
  129. PCI::HardwareID { 0x10bd, 0x0e34 }, // SureCom NE34
  130. PCI::HardwareID { 0x1050, 0x5a5a }, // Winbond W89C940F
  131. PCI::HardwareID { 0x12c3, 0x0058 }, // Holtek HT80232
  132. PCI::HardwareID { 0x12c3, 0x5598 }, // Holtek HT80229
  133. PCI::HardwareID { 0x8c4a, 0x1980 }, // Winbond W89C940 (misprogrammed)
  134. };
  135. if (!ne2k_ids.span().contains_slow(pci_device_identifier.hardware_id()))
  136. return {};
  137. u8 irq = pci_device_identifier.interrupt_line().value();
  138. // FIXME: Better propagate errors here
  139. auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
  140. if (interface_name_or_error.is_error())
  141. return {};
  142. return adopt_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
  143. }
  144. UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
  145. : NetworkAdapter(move(interface_name))
  146. , PCI::Device(address)
  147. , IRQHandler(irq)
  148. , m_io_base(PCI::get_BAR0(pci_address()) & ~3)
  149. {
  150. dmesgln("NE2000: Found @ {}", pci_address());
  151. dmesgln("NE2000: Port base: {}", m_io_base);
  152. dmesgln("NE2000: Interrupt line: {}", interrupt_number());
  153. int ram_errors = ram_test();
  154. dmesgln("NE2000: RAM test {}, got {} byte errors", (ram_errors == 0 ? "OK" : "KO"), ram_errors);
  155. reset();
  156. set_mac_address(m_mac_address);
  157. dmesgln("NE2000: MAC address: {}", m_mac_address.to_string());
  158. enable_irq();
  159. }
  160. UNMAP_AFTER_INIT NE2000NetworkAdapter::~NE2000NetworkAdapter() = default;
  161. bool NE2000NetworkAdapter::handle_irq(const RegisterState&)
  162. {
  163. u8 status = in8(REG_RW_INTERRUPTSTATUS);
  164. m_entropy_source.add_random_event(status);
  165. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Got interrupt, status={:#02x}", status);
  166. if (status == 0) {
  167. return false;
  168. }
  169. if (status & BIT_INTERRUPTMASK_PRX) {
  170. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Interrupt for packet received");
  171. }
  172. if (status & BIT_INTERRUPTMASK_PTX) {
  173. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Interrupt for packet sent");
  174. }
  175. if (status & BIT_INTERRUPTMASK_RXE) {
  176. u8 fae = in8(REG_RD_FAE_TALLY);
  177. u8 crc = in8(REG_RD_CRC_TALLY);
  178. u8 miss = in8(REG_RD_MISS_PKT_TALLY);
  179. dmesgln("NE2000NetworkAdapter: Packet reception error framing={} crc={} missed={}", fae, crc, miss);
  180. // TODO: handle counters
  181. }
  182. if (status & BIT_INTERRUPTMASK_TXE) {
  183. dmesgln("NE2000NetworkAdapter: Packet transmission error");
  184. }
  185. if (status & BIT_INTERRUPTMASK_OVW) {
  186. dmesgln("NE2000NetworkAdapter: Ring buffer reception overflow error");
  187. // TODO: handle counters
  188. }
  189. if (status & BIT_INTERRUPTMASK_CNT) {
  190. dmesgln("NE2000NetworkAdapter: Counter overflow error");
  191. // TODO: handle counters
  192. }
  193. if (status & BIT_INTERRUPTMASK_RST) {
  194. dmesgln("NE2000NetworkAdapter: NIC requires reset due to packet reception overflow");
  195. // TODO: proper reset procedure
  196. reset();
  197. }
  198. receive();
  199. m_wait_queue.wake_all();
  200. out8(REG_RW_INTERRUPTSTATUS, status);
  201. return true;
  202. }
  203. UNMAP_AFTER_INIT int NE2000NetworkAdapter::ram_test()
  204. {
  205. IOAddress io(PCI::get_BAR0(pci_address()) & ~3);
  206. int errors = 0;
  207. out8(REG_RW_COMMAND, BIT_COMMAND_DMA_ABORT | BIT_COMMAND_STOP);
  208. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  209. out8(REG_WR_DATACONFIGURATION, BIT_DATACONFIGURATION_FIFO_8B | BIT_DATACONFIGURATION_WTS);
  210. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  211. out8(REG_WR_DATACONFIGURATION, BIT_DATACONFIGURATION_FIFO_8B | BIT_DATACONFIGURATION_BOS | BIT_DATACONFIGURATION_WTS);
  212. #else
  213. # error Unknown byte order
  214. #endif
  215. out8(REG_WR_REMOTEBYTECOUNT0, 0x00);
  216. out8(REG_WR_REMOTEBYTECOUNT1, 0x00);
  217. out8(REG_WR_RECEIVECONFIGURATION, BIT_RECEIVECONFIGURATION_MON);
  218. out8(REG_RW_COMMAND, BIT_COMMAND_DMA_ABORT | BIT_COMMAND_START);
  219. Array<u8, NE2K_RAM_SIZE> buffer;
  220. const u8 patterns[3] = { 0x5a, 0xff, 0x00 };
  221. for (int i = 0; i < 3; ++i) {
  222. for (size_t j = 0; j < buffer.size(); ++j)
  223. buffer[j] = patterns[i];
  224. rdma_write(NE2K_RAM_BEGIN, buffer);
  225. rdma_read(NE2K_RAM_BEGIN, buffer);
  226. for (size_t j = 0; j < buffer.size(); ++j) {
  227. if (buffer[j] != patterns[i]) {
  228. if (errors < 16)
  229. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Bad adapter RAM @ {} expected={} got={}", PhysicalAddress(NE2K_RAM_BEGIN + j), patterns[i], buffer[j]);
  230. else if (errors == 16)
  231. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Too many RAM errors, silencing further output");
  232. errors++;
  233. }
  234. }
  235. }
  236. return errors;
  237. }
  238. void NE2000NetworkAdapter::reset()
  239. {
  240. const u8 interrupt_mask = BIT_INTERRUPTMASK_PRX | BIT_INTERRUPTMASK_PTX | BIT_INTERRUPTMASK_RXE | BIT_INTERRUPTMASK_TXE | BIT_INTERRUPTMASK_OVW | BIT_INTERRUPTMASK_CNT;
  241. u8 prom[32];
  242. // Taken from DP8390D's datasheet section 11.0, "Initialization Procedures"
  243. out8(REG_RW_COMMAND, BIT_COMMAND_DMA_ABORT | BIT_COMMAND_STOP);
  244. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  245. out8(REG_WR_DATACONFIGURATION, BIT_DATACONFIGURATION_FIFO_8B | BIT_DATACONFIGURATION_WTS);
  246. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  247. out8(REG_WR_DATACONFIGURATION, BIT_DATACONFIGURATION_FIFO_8B | BIT_DATACONFIGURATION_BOS | BIT_DATACONFIGURATION_WTS);
  248. #else
  249. # error Unknown byte order
  250. #endif
  251. out8(REG_WR_REMOTEBYTECOUNT0, 0x00);
  252. out8(REG_WR_REMOTEBYTECOUNT1, 0x00);
  253. out8(REG_WR_RECEIVECONFIGURATION, BIT_RECEIVECONFIGURATION_AB | BIT_RECEIVECONFIGURATION_AR);
  254. out8(REG_WR_TRANSMITCONFIGURATION, BIT_WR_TRANSMITCONFIGURATION_LOOPBACK);
  255. m_ring_read_ptr = NE2K_RAM_RECV_BEGIN >> 8;
  256. out8(REG_WR_PAGESTART, NE2K_RAM_RECV_BEGIN >> 8);
  257. out8(REG_RW_BOUNDARY, NE2K_RAM_RECV_BEGIN >> 8);
  258. out8(REG_WR_PAGESTOP, NE2K_RAM_RECV_END >> 8);
  259. out8(REG_RW_INTERRUPTSTATUS, 0xff);
  260. out8(REG_WR_INTERRUPTMASK, interrupt_mask);
  261. rdma_read(0, Bytes(prom, sizeof(prom)));
  262. for (int i = 0; i < 6; i++) {
  263. m_mac_address[i] = prom[i * 2];
  264. }
  265. out8(REG_RW_COMMAND, BIT_COMMAND_PAGE1 | BIT_COMMAND_DMA_ABORT | BIT_COMMAND_STOP);
  266. for (int i = 0; i < 6; i++) {
  267. out8(REG_RW_PHYSICALADDRESS0 + i, m_mac_address[i]);
  268. }
  269. out8(REG_RW_CURRENT, NE2K_RAM_RECV_BEGIN >> 8);
  270. out8(REG_RW_COMMAND, BIT_COMMAND_DMA_ABORT | BIT_COMMAND_START);
  271. out8(REG_WR_TRANSMITCONFIGURATION, 0xe0);
  272. }
  273. void NE2000NetworkAdapter::rdma_read(size_t address, Bytes payload)
  274. {
  275. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: DMA read @ {} length={}", PhysicalAddress(address), payload.size());
  276. u8 command = in8(REG_RW_COMMAND) & ~(BIT_COMMAND_PAGE_FIELD | BIT_COMMAND_DMA_FIELD);
  277. out8(REG_RW_COMMAND, command | BIT_COMMAND_DMA_ABORT);
  278. out8(REG_RW_INTERRUPTSTATUS, BIT_INTERRUPTMASK_RDC);
  279. out8(REG_WR_REMOTEBYTECOUNT0, payload.size());
  280. out8(REG_WR_REMOTEBYTECOUNT1, payload.size() >> 8);
  281. out8(REG_WR_REMOTESTARTADDRESS0, address);
  282. out8(REG_WR_REMOTESTARTADDRESS1, address >> 8);
  283. command = in8(REG_RW_COMMAND) & ~(BIT_COMMAND_DMA_FIELD);
  284. out8(REG_RW_COMMAND, command | BIT_COMMAND_DMA_READ);
  285. for (size_t i = 0; i < payload.size(); i += 2) {
  286. u16 data = in16(REG_RW_IOPORT);
  287. payload[i] = data;
  288. if (i != payload.size() - 1)
  289. payload[i + 1] = data >> 8;
  290. }
  291. while (!(in8(REG_RW_INTERRUPTSTATUS) & BIT_INTERRUPTMASK_RDC))
  292. ;
  293. }
  294. void NE2000NetworkAdapter::rdma_write(size_t address, ReadonlyBytes payload)
  295. {
  296. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: DMA write @ {} length={}", PhysicalAddress(address), payload.size());
  297. u8 command = in8(REG_RW_COMMAND) & ~(BIT_COMMAND_PAGE_FIELD | BIT_COMMAND_DMA_FIELD);
  298. out8(REG_RW_COMMAND, command | BIT_COMMAND_DMA_ABORT);
  299. out8(REG_RW_INTERRUPTSTATUS, BIT_INTERRUPTMASK_RDC);
  300. out8(REG_WR_REMOTEBYTECOUNT0, payload.size());
  301. out8(REG_WR_REMOTEBYTECOUNT1, payload.size() >> 8);
  302. out8(REG_WR_REMOTESTARTADDRESS0, address);
  303. out8(REG_WR_REMOTESTARTADDRESS1, address >> 8);
  304. command = in8(REG_RW_COMMAND) & ~(BIT_COMMAND_DMA_FIELD);
  305. out8(REG_RW_COMMAND, command | BIT_COMMAND_DMA_WRITE);
  306. for (size_t i = 0; i < payload.size(); i += 2) {
  307. u16 data = payload[i];
  308. if (i != payload.size() - 1)
  309. data |= payload[i + 1] << 8;
  310. out16(REG_RW_IOPORT, data);
  311. }
  312. while (!(in8(REG_RW_INTERRUPTSTATUS) & BIT_INTERRUPTMASK_RDC))
  313. ;
  314. }
  315. void NE2000NetworkAdapter::send_raw(ReadonlyBytes payload)
  316. {
  317. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Sending packet length={}", payload.size());
  318. if (payload.size() > NE2K_RAM_SEND_SIZE) {
  319. dmesgln("NE2000NetworkAdapter: Packet to send was too big; discarding");
  320. return;
  321. }
  322. while (in8(REG_RW_COMMAND) & BIT_COMMAND_TXP)
  323. m_wait_queue.wait_forever("NE2000NetworkAdapter");
  324. disable_irq();
  325. size_t packet_size = payload.size();
  326. if (packet_size < 64)
  327. packet_size = 64;
  328. rdma_write(NE2K_RAM_SEND_BEGIN, payload);
  329. out8(REG_WR_TRANSMITPAGE, NE2K_RAM_SEND_BEGIN >> 8);
  330. out8(REG_WR_TRANSMITBYTECOUNT0, packet_size);
  331. out8(REG_WR_TRANSMITBYTECOUNT1, packet_size >> 8);
  332. out8(REG_RW_COMMAND, BIT_COMMAND_DMA_ABORT | BIT_COMMAND_TXP | BIT_COMMAND_START);
  333. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Packet submitted for transmission");
  334. enable_irq();
  335. }
  336. void NE2000NetworkAdapter::receive()
  337. {
  338. while (true) {
  339. out8(REG_RW_COMMAND, BIT_COMMAND_PAGE1 | in8(REG_RW_COMMAND));
  340. u8 current = in8(REG_RW_CURRENT);
  341. out8(REG_RW_COMMAND, in8(REG_RW_COMMAND) & ~BIT_COMMAND_PAGE_FIELD);
  342. if (m_ring_read_ptr == current)
  343. break;
  344. size_t header_address = m_ring_read_ptr << 8;
  345. received_packet_header header;
  346. rdma_read(header_address, Bytes(reinterpret_cast<u8*>(&header), sizeof(header)));
  347. bool packet_ok = header.status & BIT_RECEIVESTATUS_PRX;
  348. dbgln_if(NE2000_DEBUG, "NE2000NetworkAdapter: Packet received {} length={}", (packet_ok ? "intact" : "damaged"), header.length);
  349. if (packet_ok) {
  350. size_t bytes_in_packet = sizeof(received_packet_header) + header.length;
  351. auto packet_result = NetworkByteBuffer::create_uninitialized(bytes_in_packet);
  352. u8 drop_buffer[NE2K_PAGE_SIZE];
  353. Bytes buffer { drop_buffer, array_size(drop_buffer) };
  354. bool will_drop { false };
  355. if (packet_result.is_error()) {
  356. dbgln("NE2000NetworkAdapter: Not enough memory for packet with length = {}, dropping.", header.length);
  357. will_drop = true;
  358. } else {
  359. buffer = packet_result.value().bytes();
  360. }
  361. int current_offset = 0;
  362. int ring_offset = header_address;
  363. while (bytes_in_packet > 0) {
  364. int copy_size = min(bytes_in_packet, NE2K_PAGE_SIZE);
  365. rdma_read(ring_offset, buffer.slice(current_offset, copy_size));
  366. if (!will_drop)
  367. current_offset += copy_size;
  368. ring_offset += copy_size;
  369. bytes_in_packet -= copy_size;
  370. if (ring_offset == NE2K_RAM_RECV_END)
  371. ring_offset = NE2K_RAM_RECV_BEGIN;
  372. }
  373. if (!will_drop)
  374. did_receive(buffer.slice(sizeof(received_packet_header)));
  375. }
  376. if (header.next_packet_page == (NE2K_RAM_RECV_BEGIN >> 8))
  377. out8(REG_RW_BOUNDARY, (NE2K_RAM_RECV_END >> 8) - 1);
  378. else
  379. out8(REG_RW_BOUNDARY, header.next_packet_page - 1);
  380. m_ring_read_ptr = header.next_packet_page;
  381. }
  382. }
  383. void NE2000NetworkAdapter::out8(u16 address, u8 data)
  384. {
  385. m_io_base.offset(address).out(data);
  386. }
  387. void NE2000NetworkAdapter::out16(u16 address, u16 data)
  388. {
  389. m_io_base.offset(address).out(data);
  390. }
  391. u8 NE2000NetworkAdapter::in8(u16 address)
  392. {
  393. u8 data = m_io_base.offset(address).in<u8>();
  394. return data;
  395. }
  396. u16 NE2000NetworkAdapter::in16(u16 address)
  397. {
  398. return m_io_base.offset(address).in<u16>();
  399. }
  400. }