NE2000NetworkAdapter.cpp 17 KB

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