RTL8168NetworkAdapter.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullOwnPtrVector.h>
  8. #include <AK/OwnPtr.h>
  9. #include <Kernel/Bus/PCI/Access.h>
  10. #include <Kernel/Bus/PCI/Device.h>
  11. #include <Kernel/IO.h>
  12. #include <Kernel/Interrupts/IRQHandler.h>
  13. #include <Kernel/Net/NetworkAdapter.h>
  14. #include <Kernel/Random.h>
  15. namespace Kernel {
  16. // RTL8618 / RTL8111 Driver based on https://people.freebsd.org/~wpaul/RealTek/RTL8111B_8168B_Registers_DataSheet_1.0.pdf
  17. class RTL8168NetworkAdapter final : public NetworkAdapter
  18. , public PCI::Device
  19. , public IRQHandler {
  20. public:
  21. static RefPtr<RTL8168NetworkAdapter> try_to_initialize(PCI::Address);
  22. virtual ~RTL8168NetworkAdapter() override;
  23. virtual void send_raw(ReadonlyBytes) override;
  24. virtual bool link_up() override { return m_link_up; }
  25. virtual bool link_full_duplex() override;
  26. virtual i32 link_speed() override;
  27. virtual StringView purpose() const override { return class_name(); }
  28. private:
  29. // FIXME: should this be increased? (maximum allowed here is 1024) - memory usage vs packet loss chance tradeoff
  30. static const size_t number_of_rx_descriptors = 64;
  31. static const size_t number_of_tx_descriptors = 16;
  32. RTL8168NetworkAdapter(PCI::Address, u8 irq);
  33. virtual bool handle_irq(const RegisterState&) override;
  34. virtual StringView class_name() const override { return "RTL8168NetworkAdapter"sv; }
  35. struct [[gnu::packed]] TXDescriptor {
  36. volatile u16 frame_length; // top 2 bits are reserved
  37. volatile u16 flags;
  38. volatile u16 vlan_tag;
  39. volatile u16 vlan_flags;
  40. volatile u32 buffer_address_low;
  41. volatile u32 buffer_address_high;
  42. // flags bit field
  43. static constexpr u16 Ownership = 0x8000u;
  44. static constexpr u16 EndOfRing = 0x4000u;
  45. static constexpr u16 FirstSegment = 0x2000u;
  46. static constexpr u16 LastSegment = 0x1000u;
  47. static constexpr u16 LargeSend = 0x800u;
  48. };
  49. static_assert(sizeof(TXDescriptor) == 16u);
  50. struct [[gnu::packed]] RXDescriptor {
  51. volatile u16 buffer_size; // top 2 bits are reserved
  52. volatile u16 flags;
  53. volatile u16 vlan_tag;
  54. volatile u16 vlan_flags;
  55. volatile u32 buffer_address_low;
  56. volatile u32 buffer_address_high;
  57. // flags bit field
  58. static constexpr u16 Ownership = 0x8000u;
  59. static constexpr u16 EndOfRing = 0x4000u;
  60. static constexpr u16 FirstSegment = 0x2000u;
  61. static constexpr u16 LastSegment = 0x1000u;
  62. static constexpr u16 MulticastPacket = 0x800u;
  63. static constexpr u16 PhysicalPacket = 0x400u;
  64. static constexpr u16 BroadcastPacket = 0x200u;
  65. static constexpr u16 WatchdogTimerExpired = 0x40;
  66. static constexpr u16 ErrorSummary = 0x20;
  67. static constexpr u16 RuntPacket = 0x10;
  68. static constexpr u16 CRCError = 0x8;
  69. };
  70. static_assert(sizeof(RXDescriptor) == 16u);
  71. enum class ChipVersion : u8 {
  72. Unknown = 0,
  73. Version1 = 1,
  74. Version2 = 2,
  75. Version3 = 3,
  76. Version4 = 4,
  77. Version5 = 5,
  78. Version6 = 6,
  79. Version7 = 7,
  80. Version8 = 8,
  81. Version9 = 9,
  82. Version10 = 10,
  83. Version11 = 11,
  84. Version12 = 12,
  85. Version13 = 13,
  86. Version14 = 14,
  87. Version15 = 15,
  88. Version16 = 16,
  89. Version17 = 17,
  90. Version18 = 18,
  91. Version19 = 19,
  92. Version20 = 20,
  93. Version21 = 21,
  94. Version22 = 22,
  95. Version23 = 23,
  96. Version24 = 24,
  97. Version25 = 25,
  98. Version26 = 26,
  99. Version27 = 27,
  100. Version28 = 28,
  101. Version29 = 29,
  102. Version30 = 30
  103. };
  104. void identify_chip_version();
  105. String possible_device_name();
  106. void reset();
  107. void read_mac_address();
  108. void set_phy_speed();
  109. void start_hardware();
  110. void initialize();
  111. void startup();
  112. void configure_phy();
  113. void configure_phy_b_1();
  114. void configure_phy_b_2();
  115. void configure_phy_e_2();
  116. void configure_phy_h_1();
  117. void configure_phy_h_2();
  118. void rar_exgmac_set();
  119. void hardware_quirks();
  120. void hardware_quirks_b_1();
  121. void hardware_quirks_b_2();
  122. void hardware_quirks_e_2();
  123. void hardware_quirks_h();
  124. void initialize_rx_descriptors();
  125. void initialize_tx_descriptors();
  126. void receive();
  127. void out8(u16 address, u8 data);
  128. void out16(u16 address, u16 data);
  129. void out32(u16 address, u32 data);
  130. void out64(u16 address, u64 data);
  131. u8 in8(u16 address);
  132. u16 in16(u16 address);
  133. u32 in32(u16 address);
  134. void phy_out(u8 address, u16 data);
  135. u16 phy_in(u8 address);
  136. void phy_update(u32 address, u32 set, u32 clear);
  137. struct PhyRegister {
  138. u16 address;
  139. u16 data;
  140. };
  141. void phy_out_batch(const PhyRegister[], size_t length);
  142. void extended_phy_out(u8 address, u16 data);
  143. u16 extended_phy_in(u8 address);
  144. struct EPhyUpdate {
  145. u32 offset;
  146. u16 clear;
  147. u16 set;
  148. };
  149. void extended_phy_initialize(const EPhyUpdate[], size_t length);
  150. void eri_out(u32 address, u32 mask, u32 data, u32 type);
  151. u32 eri_in(u32 address, u32 type);
  152. void eri_update(u32 address, u32 mask, u32 set, u32 clear, u32 type);
  153. struct ExgMacRegister {
  154. u16 address;
  155. u16 mask;
  156. u32 value;
  157. };
  158. void exgmac_out_batch(const ExgMacRegister[], size_t length);
  159. void csi_out(u32 address, u32 data);
  160. u32 csi_in(u32 address);
  161. void csi_enable(u32 bits);
  162. void ocp_out(u32 address, u32 data);
  163. u32 ocp_in(u32 address);
  164. void ocp_phy_out(u32 address, u32 data);
  165. u16 ocp_phy_in(u32 address);
  166. ChipVersion m_version { ChipVersion::Unknown };
  167. bool m_version_uncertain { true };
  168. IOAddress m_io_base;
  169. u32 m_ocp_base_address { 0 };
  170. OwnPtr<Memory::Region> m_rx_descriptors_region;
  171. NonnullOwnPtrVector<Memory::Region> m_rx_buffers_regions;
  172. u16 m_rx_free_index { 0 };
  173. OwnPtr<Memory::Region> m_tx_descriptors_region;
  174. NonnullOwnPtrVector<Memory::Region> m_tx_buffers_regions;
  175. u16 m_tx_free_index { 0 };
  176. bool m_link_up { false };
  177. EntropySource m_entropy_source;
  178. WaitQueue m_wait_queue;
  179. };
  180. }