E1000NetworkAdapter.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/MACAddress.h>
  7. #include <Kernel/Bus/PCI/API.h>
  8. #include <Kernel/Bus/PCI/IDs.h>
  9. #include <Kernel/Debug.h>
  10. #include <Kernel/Net/E1000NetworkAdapter.h>
  11. #include <Kernel/Net/NetworkingManagement.h>
  12. #include <Kernel/Sections.h>
  13. namespace Kernel {
  14. #define REG_CTRL 0x0000
  15. #define REG_STATUS 0x0008
  16. #define REG_EEPROM 0x0014
  17. #define REG_CTRL_EXT 0x0018
  18. #define REG_INTERRUPT_CAUSE_READ 0x00C0
  19. #define REG_INTERRUPT_RATE 0x00C4
  20. #define REG_INTERRUPT_MASK_SET 0x00D0
  21. #define REG_INTERRUPT_MASK_CLEAR 0x00D8
  22. #define REG_RCTRL 0x0100
  23. #define REG_RXDESCLO 0x2800
  24. #define REG_RXDESCHI 0x2804
  25. #define REG_RXDESCLEN 0x2808
  26. #define REG_RXDESCHEAD 0x2810
  27. #define REG_RXDESCTAIL 0x2818
  28. #define REG_TCTRL 0x0400
  29. #define REG_TXDESCLO 0x3800
  30. #define REG_TXDESCHI 0x3804
  31. #define REG_TXDESCLEN 0x3808
  32. #define REG_TXDESCHEAD 0x3810
  33. #define REG_TXDESCTAIL 0x3818
  34. #define REG_RDTR 0x2820 // RX Delay Timer Register
  35. #define REG_RXDCTL 0x3828 // RX Descriptor Control
  36. #define REG_RADV 0x282C // RX Int. Absolute Delay Timer
  37. #define REG_RSRPD 0x2C00 // RX Small Packet Detect Interrupt
  38. #define REG_TIPG 0x0410 // Transmit Inter Packet Gap
  39. #define ECTRL_SLU 0x40 //set link up
  40. #define RCTL_EN (1 << 1) // Receiver Enable
  41. #define RCTL_SBP (1 << 2) // Store Bad Packets
  42. #define RCTL_UPE (1 << 3) // Unicast Promiscuous Enabled
  43. #define RCTL_MPE (1 << 4) // Multicast Promiscuous Enabled
  44. #define RCTL_LPE (1 << 5) // Long Packet Reception Enable
  45. #define RCTL_LBM_NONE (0 << 6) // No Loopback
  46. #define RCTL_LBM_PHY (3 << 6) // PHY or external SerDesc loopback
  47. #define RTCL_RDMTS_HALF (0 << 8) // Free Buffer Threshold is 1/2 of RDLEN
  48. #define RTCL_RDMTS_QUARTER (1 << 8) // Free Buffer Threshold is 1/4 of RDLEN
  49. #define RTCL_RDMTS_EIGHTH (2 << 8) // Free Buffer Threshold is 1/8 of RDLEN
  50. #define RCTL_MO_36 (0 << 12) // Multicast Offset - bits 47:36
  51. #define RCTL_MO_35 (1 << 12) // Multicast Offset - bits 46:35
  52. #define RCTL_MO_34 (2 << 12) // Multicast Offset - bits 45:34
  53. #define RCTL_MO_32 (3 << 12) // Multicast Offset - bits 43:32
  54. #define RCTL_BAM (1 << 15) // Broadcast Accept Mode
  55. #define RCTL_VFE (1 << 18) // VLAN Filter Enable
  56. #define RCTL_CFIEN (1 << 19) // Canonical Form Indicator Enable
  57. #define RCTL_CFI (1 << 20) // Canonical Form Indicator Bit Value
  58. #define RCTL_DPF (1 << 22) // Discard Pause Frames
  59. #define RCTL_PMCF (1 << 23) // Pass MAC Control Frames
  60. #define RCTL_SECRC (1 << 26) // Strip Ethernet CRC
  61. // Buffer Sizes
  62. #define RCTL_BSIZE_256 (3 << 16)
  63. #define RCTL_BSIZE_512 (2 << 16)
  64. #define RCTL_BSIZE_1024 (1 << 16)
  65. #define RCTL_BSIZE_2048 (0 << 16)
  66. #define RCTL_BSIZE_4096 ((3 << 16) | (1 << 25))
  67. #define RCTL_BSIZE_8192 ((2 << 16) | (1 << 25))
  68. #define RCTL_BSIZE_16384 ((1 << 16) | (1 << 25))
  69. // Transmit Command
  70. #define CMD_EOP (1 << 0) // End of Packet
  71. #define CMD_IFCS (1 << 1) // Insert FCS
  72. #define CMD_IC (1 << 2) // Insert Checksum
  73. #define CMD_RS (1 << 3) // Report Status
  74. #define CMD_RPS (1 << 4) // Report Packet Sent
  75. #define CMD_VLE (1 << 6) // VLAN Packet Enable
  76. #define CMD_IDE (1 << 7) // Interrupt Delay Enable
  77. // TCTL Register
  78. #define TCTL_EN (1 << 1) // Transmit Enable
  79. #define TCTL_PSP (1 << 3) // Pad Short Packets
  80. #define TCTL_CT_SHIFT 4 // Collision Threshold
  81. #define TCTL_COLD_SHIFT 12 // Collision Distance
  82. #define TCTL_SWXOFF (1 << 22) // Software XOFF Transmission
  83. #define TCTL_RTLC (1 << 24) // Re-transmit on Late Collision
  84. #define TSTA_DD (1 << 0) // Descriptor Done
  85. #define TSTA_EC (1 << 1) // Excess Collisions
  86. #define TSTA_LC (1 << 2) // Late Collision
  87. #define LSTA_TU (1 << 3) // Transmit Underrun
  88. // STATUS Register
  89. #define STATUS_FD 0x01
  90. #define STATUS_LU 0x02
  91. #define STATUS_TXOFF 0x08
  92. #define STATUS_SPEED 0xC0
  93. #define STATUS_SPEED_10MB 0x00
  94. #define STATUS_SPEED_100MB 0x40
  95. #define STATUS_SPEED_1000MB1 0x80
  96. #define STATUS_SPEED_1000MB2 0xC0
  97. // Interrupt Masks
  98. #define INTERRUPT_TXDW (1 << 0)
  99. #define INTERRUPT_TXQE (1 << 1)
  100. #define INTERRUPT_LSC (1 << 2)
  101. #define INTERRUPT_RXSEQ (1 << 3)
  102. #define INTERRUPT_RXDMT0 (1 << 4)
  103. #define INTERRUPT_RXO (1 << 6)
  104. #define INTERRUPT_RXT0 (1 << 7)
  105. #define INTERRUPT_MDAC (1 << 9)
  106. #define INTERRUPT_RXCFG (1 << 10)
  107. #define INTERRUPT_PHYINT (1 << 12)
  108. #define INTERRUPT_TXD_LOW (1 << 15)
  109. #define INTERRUPT_SRPD (1 << 16)
  110. // https://www.intel.com/content/dam/doc/manual/pci-pci-x-family-gbe-controllers-software-dev-manual.pdf Section 5.2
  111. UNMAP_AFTER_INIT static bool is_valid_device_id(u16 device_id)
  112. {
  113. // FIXME: It would be nice to distinguish which particular device it is.
  114. // Especially since it's needed to determine which registers we can access.
  115. // The reason I haven't done it now is because there's some IDs with multiple devices
  116. // and some devices with multiple IDs.
  117. switch (device_id) {
  118. case 0x1019: // 82547EI-A0, 82547EI-A1, 82547EI-B0, 82547GI-B0
  119. case 0x101A: // 82547EI-B0
  120. case 0x1010: // 82546EB-A1
  121. case 0x1012: // 82546EB-A1
  122. case 0x101D: // 82546EB-A1
  123. case 0x1079: // 82546GB-B0
  124. case 0x107A: // 82546GB-B0
  125. case 0x107B: // 82546GB-B0
  126. case 0x100F: // 82545EM-A
  127. case 0x1011: // 82545EM-A
  128. case 0x1026: // 82545GM-B
  129. case 0x1027: // 82545GM-B
  130. case 0x1028: // 82545GM-B
  131. case 0x1107: // 82544EI-A4
  132. case 0x1112: // 82544GC-A4
  133. case 0x1013: // 82541EI-A0, 82541EI-B0
  134. case 0x1018: // 82541EI-B0
  135. case 0x1076: // 82541GI-B1, 82541PI-C0
  136. case 0x1077: // 82541GI-B1
  137. case 0x1078: // 82541ER-C0
  138. case 0x1017: // 82540EP-A
  139. case 0x1016: // 82540EP-A
  140. case 0x100E: // 82540EM-A
  141. case 0x1015: // 82540EM-A
  142. return true;
  143. default:
  144. return false;
  145. }
  146. }
  147. UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initialize(PCI::DeviceIdentifier const& pci_device_identifier)
  148. {
  149. if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Intel)
  150. return {};
  151. if (!is_valid_device_id(pci_device_identifier.hardware_id().device_id))
  152. return {};
  153. u8 irq = pci_device_identifier.interrupt_line().value();
  154. // FIXME: Better propagate errors here
  155. auto interface_name_or_error = NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier);
  156. if (interface_name_or_error.is_error())
  157. return {};
  158. auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(pci_device_identifier.address(), irq, interface_name_or_error.release_value()));
  159. if (!adapter)
  160. return {};
  161. if (adapter->initialize())
  162. return adapter;
  163. return {};
  164. }
  165. UNMAP_AFTER_INIT void E1000NetworkAdapter::setup_link()
  166. {
  167. u32 flags = in32(REG_CTRL);
  168. out32(REG_CTRL, flags | ECTRL_SLU);
  169. }
  170. UNMAP_AFTER_INIT void E1000NetworkAdapter::setup_interrupts()
  171. {
  172. out32(REG_INTERRUPT_RATE, 6000); // Interrupt rate of 1.536 milliseconds
  173. out32(REG_INTERRUPT_MASK_SET, INTERRUPT_LSC | INTERRUPT_RXT0 | INTERRUPT_RXO);
  174. in32(REG_INTERRUPT_CAUSE_READ);
  175. enable_irq();
  176. }
  177. UNMAP_AFTER_INIT bool E1000NetworkAdapter::initialize()
  178. {
  179. dmesgln("E1000: Found @ {}", pci_address());
  180. enable_bus_mastering(pci_address());
  181. m_io_base = IOAddress(PCI::get_BAR1(pci_address()) & ~1);
  182. size_t mmio_base_size = PCI::get_BAR_space_size(pci_address(), 0);
  183. auto region_or_error = MM.allocate_kernel_region(PhysicalAddress(page_base_of(PCI::get_BAR0(pci_address()))), Memory::page_round_up(mmio_base_size), "E1000 MMIO", Memory::Region::Access::ReadWrite, Memory::Region::Cacheable::No);
  184. if (region_or_error.is_error())
  185. return false;
  186. m_mmio_region = region_or_error.release_value();
  187. m_mmio_base = m_mmio_region->vaddr();
  188. m_use_mmio = true;
  189. dmesgln("E1000: port base: {}", m_io_base);
  190. dmesgln("E1000: MMIO base: {}", PhysicalAddress(PCI::get_BAR0(pci_address()) & 0xfffffffc));
  191. dmesgln("E1000: MMIO base size: {} bytes", mmio_base_size);
  192. dmesgln("E1000: Interrupt line: {}", interrupt_number());
  193. detect_eeprom();
  194. dmesgln("E1000: Has EEPROM? {}", m_has_eeprom);
  195. read_mac_address();
  196. const auto& mac = mac_address();
  197. dmesgln("E1000: MAC address: {}", mac.to_string());
  198. initialize_rx_descriptors();
  199. initialize_tx_descriptors();
  200. setup_link();
  201. setup_interrupts();
  202. return true;
  203. }
  204. UNMAP_AFTER_INIT E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq, NonnullOwnPtr<KString> interface_name)
  205. : NetworkAdapter(move(interface_name))
  206. , PCI::Device(address)
  207. , IRQHandler(irq)
  208. , m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_rx_desc) * number_of_rx_descriptors + 16), "E1000 RX Descriptors", Memory::Region::Access::ReadWrite).release_value())
  209. , m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(e1000_tx_desc) * number_of_tx_descriptors + 16), "E1000 TX Descriptors", Memory::Region::Access::ReadWrite).release_value())
  210. {
  211. }
  212. UNMAP_AFTER_INIT E1000NetworkAdapter::~E1000NetworkAdapter()
  213. {
  214. }
  215. bool E1000NetworkAdapter::handle_irq(const RegisterState&)
  216. {
  217. u32 status = in32(REG_INTERRUPT_CAUSE_READ);
  218. m_entropy_source.add_random_event(status);
  219. if (status == 0)
  220. return false;
  221. if (status & INTERRUPT_LSC) {
  222. u32 flags = in32(REG_CTRL);
  223. out32(REG_CTRL, flags | ECTRL_SLU);
  224. }
  225. if (status & INTERRUPT_RXDMT0) {
  226. // Threshold OK?
  227. }
  228. if (status & INTERRUPT_RXO) {
  229. dbgln_if(E1000_DEBUG, "E1000: RX buffer overrun");
  230. }
  231. if (status & INTERRUPT_RXT0) {
  232. receive();
  233. }
  234. m_wait_queue.wake_all();
  235. out32(REG_INTERRUPT_CAUSE_READ, 0xffffffff);
  236. return true;
  237. }
  238. UNMAP_AFTER_INIT void E1000NetworkAdapter::detect_eeprom()
  239. {
  240. out32(REG_EEPROM, 0x1);
  241. for (int i = 0; i < 999; ++i) {
  242. u32 data = in32(REG_EEPROM);
  243. if (data & 0x10) {
  244. m_has_eeprom = true;
  245. return;
  246. }
  247. }
  248. m_has_eeprom = false;
  249. }
  250. UNMAP_AFTER_INIT u32 E1000NetworkAdapter::read_eeprom(u8 address)
  251. {
  252. u16 data = 0;
  253. u32 tmp = 0;
  254. if (m_has_eeprom) {
  255. out32(REG_EEPROM, ((u32)address << 8) | 1);
  256. while (!((tmp = in32(REG_EEPROM)) & (1 << 4)))
  257. ;
  258. } else {
  259. out32(REG_EEPROM, ((u32)address << 2) | 1);
  260. while (!((tmp = in32(REG_EEPROM)) & (1 << 1)))
  261. ;
  262. }
  263. data = (tmp >> 16) & 0xffff;
  264. return data;
  265. }
  266. UNMAP_AFTER_INIT void E1000NetworkAdapter::read_mac_address()
  267. {
  268. if (m_has_eeprom) {
  269. MACAddress mac {};
  270. u32 tmp = read_eeprom(0);
  271. mac[0] = tmp & 0xff;
  272. mac[1] = tmp >> 8;
  273. tmp = read_eeprom(1);
  274. mac[2] = tmp & 0xff;
  275. mac[3] = tmp >> 8;
  276. tmp = read_eeprom(2);
  277. mac[4] = tmp & 0xff;
  278. mac[5] = tmp >> 8;
  279. set_mac_address(mac);
  280. } else {
  281. VERIFY_NOT_REACHED();
  282. }
  283. }
  284. bool E1000NetworkAdapter::link_up()
  285. {
  286. return (in32(REG_STATUS) & STATUS_LU);
  287. }
  288. UNMAP_AFTER_INIT void E1000NetworkAdapter::initialize_rx_descriptors()
  289. {
  290. auto* rx_descriptors = (e1000_tx_desc*)m_rx_descriptors_region->vaddr().as_ptr();
  291. constexpr auto rx_buffer_size = 8192;
  292. constexpr auto rx_buffer_page_count = rx_buffer_size / PAGE_SIZE;
  293. m_rx_buffer_region = MM.allocate_contiguous_kernel_region(rx_buffer_size * number_of_rx_descriptors, "E1000 RX buffers", Memory::Region::Access::ReadWrite).release_value();
  294. for (size_t i = 0; i < number_of_rx_descriptors; ++i) {
  295. auto& descriptor = rx_descriptors[i];
  296. m_rx_buffers[i] = m_rx_buffer_region->vaddr().as_ptr() + rx_buffer_size * i;
  297. descriptor.addr = m_rx_buffer_region->physical_page(rx_buffer_page_count * i)->paddr().get();
  298. descriptor.status = 0;
  299. }
  300. out32(REG_RXDESCLO, m_rx_descriptors_region->physical_page(0)->paddr().get());
  301. out32(REG_RXDESCHI, 0);
  302. out32(REG_RXDESCLEN, number_of_rx_descriptors * sizeof(e1000_rx_desc));
  303. out32(REG_RXDESCHEAD, 0);
  304. out32(REG_RXDESCTAIL, number_of_rx_descriptors - 1);
  305. out32(REG_RCTRL, RCTL_EN | RCTL_SBP | RCTL_UPE | RCTL_MPE | RCTL_LBM_NONE | RTCL_RDMTS_HALF | RCTL_BAM | RCTL_SECRC | RCTL_BSIZE_8192);
  306. }
  307. UNMAP_AFTER_INIT void E1000NetworkAdapter::initialize_tx_descriptors()
  308. {
  309. auto* tx_descriptors = (e1000_tx_desc*)m_tx_descriptors_region->vaddr().as_ptr();
  310. constexpr auto tx_buffer_size = 8192;
  311. constexpr auto tx_buffer_page_count = tx_buffer_size / PAGE_SIZE;
  312. m_tx_buffer_region = MM.allocate_contiguous_kernel_region(tx_buffer_size * number_of_tx_descriptors, "E1000 TX buffers", Memory::Region::Access::ReadWrite).release_value();
  313. for (size_t i = 0; i < number_of_tx_descriptors; ++i) {
  314. auto& descriptor = tx_descriptors[i];
  315. m_tx_buffers[i] = m_tx_buffer_region->vaddr().as_ptr() + tx_buffer_size * i;
  316. descriptor.addr = m_tx_buffer_region->physical_page(tx_buffer_page_count * i)->paddr().get();
  317. descriptor.cmd = 0;
  318. }
  319. out32(REG_TXDESCLO, m_tx_descriptors_region->physical_page(0)->paddr().get());
  320. out32(REG_TXDESCHI, 0);
  321. out32(REG_TXDESCLEN, number_of_tx_descriptors * sizeof(e1000_tx_desc));
  322. out32(REG_TXDESCHEAD, 0);
  323. out32(REG_TXDESCTAIL, 0);
  324. out32(REG_TCTRL, in32(REG_TCTRL) | TCTL_EN | TCTL_PSP);
  325. out32(REG_TIPG, 0x0060200A);
  326. }
  327. void E1000NetworkAdapter::out8(u16 address, u8 data)
  328. {
  329. dbgln_if(E1000_DEBUG, "E1000: OUT8 {:#02x} @ {:#04x}", data, address);
  330. if (m_use_mmio) {
  331. auto* ptr = (volatile u8*)(m_mmio_base.get() + address);
  332. *ptr = data;
  333. return;
  334. }
  335. m_io_base.offset(address).out(data);
  336. }
  337. void E1000NetworkAdapter::out16(u16 address, u16 data)
  338. {
  339. dbgln_if(E1000_DEBUG, "E1000: OUT16 {:#04x} @ {:#04x}", data, address);
  340. if (m_use_mmio) {
  341. auto* ptr = (volatile u16*)(m_mmio_base.get() + address);
  342. *ptr = data;
  343. return;
  344. }
  345. m_io_base.offset(address).out(data);
  346. }
  347. void E1000NetworkAdapter::out32(u16 address, u32 data)
  348. {
  349. dbgln_if(E1000_DEBUG, "E1000: OUT32 {:#08x} @ {:#04x}", data, address);
  350. if (m_use_mmio) {
  351. auto* ptr = (volatile u32*)(m_mmio_base.get() + address);
  352. *ptr = data;
  353. return;
  354. }
  355. m_io_base.offset(address).out(data);
  356. }
  357. u8 E1000NetworkAdapter::in8(u16 address)
  358. {
  359. dbgln_if(E1000_DEBUG, "E1000: IN8 @ {:#04x}", address);
  360. if (m_use_mmio)
  361. return *(volatile u8*)(m_mmio_base.get() + address);
  362. return m_io_base.offset(address).in<u8>();
  363. }
  364. u16 E1000NetworkAdapter::in16(u16 address)
  365. {
  366. dbgln_if(E1000_DEBUG, "E1000: IN16 @ {:#04x}", address);
  367. if (m_use_mmio)
  368. return *(volatile u16*)(m_mmio_base.get() + address);
  369. return m_io_base.offset(address).in<u16>();
  370. }
  371. u32 E1000NetworkAdapter::in32(u16 address)
  372. {
  373. dbgln_if(E1000_DEBUG, "E1000: IN32 @ {:#04x}", address);
  374. if (m_use_mmio)
  375. return *(volatile u32*)(m_mmio_base.get() + address);
  376. return m_io_base.offset(address).in<u32>();
  377. }
  378. void E1000NetworkAdapter::send_raw(ReadonlyBytes payload)
  379. {
  380. disable_irq();
  381. size_t tx_current = in32(REG_TXDESCTAIL) % number_of_tx_descriptors;
  382. dbgln_if(E1000_DEBUG, "E1000: Sending packet ({} bytes)", payload.size());
  383. auto* tx_descriptors = (e1000_tx_desc*)m_tx_descriptors_region->vaddr().as_ptr();
  384. auto& descriptor = tx_descriptors[tx_current];
  385. VERIFY(payload.size() <= 8192);
  386. auto* vptr = (void*)m_tx_buffers[tx_current];
  387. memcpy(vptr, payload.data(), payload.size());
  388. descriptor.length = payload.size();
  389. descriptor.status = 0;
  390. descriptor.cmd = CMD_EOP | CMD_IFCS | CMD_RS;
  391. dbgln_if(E1000_DEBUG, "E1000: Using tx descriptor {} (head is at {})", tx_current, in32(REG_TXDESCHEAD));
  392. tx_current = (tx_current + 1) % number_of_tx_descriptors;
  393. cli();
  394. enable_irq();
  395. out32(REG_TXDESCTAIL, tx_current);
  396. for (;;) {
  397. if (descriptor.status) {
  398. sti();
  399. break;
  400. }
  401. m_wait_queue.wait_forever("E1000NetworkAdapter");
  402. }
  403. dbgln_if(E1000_DEBUG, "E1000: Sent packet, status is now {:#02x}!", (u8)descriptor.status);
  404. }
  405. void E1000NetworkAdapter::receive()
  406. {
  407. auto* rx_descriptors = (e1000_tx_desc*)m_rx_descriptors_region->vaddr().as_ptr();
  408. u32 rx_current;
  409. for (;;) {
  410. rx_current = in32(REG_RXDESCTAIL) % number_of_rx_descriptors;
  411. rx_current = (rx_current + 1) % number_of_rx_descriptors;
  412. if (!(rx_descriptors[rx_current].status & 1))
  413. break;
  414. auto* buffer = m_rx_buffers[rx_current];
  415. u16 length = rx_descriptors[rx_current].length;
  416. VERIFY(length <= 8192);
  417. dbgln_if(E1000_DEBUG, "E1000: Received 1 packet @ {:p} ({} bytes)", buffer, length);
  418. did_receive({ buffer, length });
  419. rx_descriptors[rx_current].status = 0;
  420. out32(REG_RXDESCTAIL, rx_current);
  421. }
  422. }
  423. i32 E1000NetworkAdapter::link_speed()
  424. {
  425. if (!link_up())
  426. return NetworkAdapter::LINKSPEED_INVALID;
  427. u32 speed = in32(REG_STATUS) & STATUS_SPEED;
  428. switch (speed) {
  429. case STATUS_SPEED_10MB:
  430. return 10;
  431. case STATUS_SPEED_100MB:
  432. return 100;
  433. case STATUS_SPEED_1000MB1:
  434. case STATUS_SPEED_1000MB2:
  435. return 1000;
  436. default:
  437. return NetworkAdapter::LINKSPEED_INVALID;
  438. }
  439. }
  440. bool E1000NetworkAdapter::link_full_duplex()
  441. {
  442. u32 status = in32(REG_STATUS);
  443. return !!(status & STATUS_FD);
  444. }
  445. }