RTL8168NetworkAdapter.cpp 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@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/NetworkingManagement.h>
  11. #include <Kernel/Net/Realtek/RTL8168NetworkAdapter.h>
  12. #include <Kernel/Sections.h>
  13. namespace Kernel {
  14. #define REG_MAC 0x00
  15. #define REG_MAR4 0x0B
  16. #define REG_MAR0 0x0F
  17. #define REG_EEE_LED 0x1B
  18. #define REG_TXADDR 0x20
  19. #define REG_COMMAND 0x37
  20. #define REG_TXSTART 0x38
  21. #define REG_IMR 0x3C
  22. #define REG_ISR 0x3E
  23. #define REG_TXCFG 0x40
  24. #define REG_RXCFG 0x44
  25. #define REG_MPC 0x4C
  26. #define REG_CFG9346 0x50
  27. #define REG_CONFIG1 0x52
  28. #define REG_CONFIG2 0x53
  29. #define REG_CONFIG3 0x54
  30. #define REG_CONFIG4 0x55
  31. #define REG_CONFIG5 0x56
  32. #define REG_MULTIINTR 0x5C
  33. #define REG_PHYACCESS 0x60
  34. #define REG_CSI_DATA 0x64
  35. #define REG_CSI_ADDR 0x68
  36. #define REG_PHYSTATUS 0x6C
  37. #define REG_PMCH 0x6F
  38. #define REG_ERI_DATA 0x70
  39. #define REG_ERI_ADDR 0x74
  40. #define REG_EPHYACCESS 0x80
  41. #define REG_OCP_DATA 0xB0
  42. #define REG_OCP_ADDR 0xB4
  43. #define REG_GPHY_OCP 0xB8
  44. #define REG_DLLPR 0xD0
  45. #define REG_MCU 0xD3
  46. #define REG_RMS 0xDA
  47. #define REG_CPLUS_COMMAND 0xE0
  48. #define REG_INT_MOD 0xE2
  49. #define REG_RXADDR 0xE4
  50. #define REG_MTPS 0xEC
  51. #define REG_MISC 0xF0
  52. #define REG_MISC2 0xF2
  53. #define REG_IBCR0 0xF8
  54. #define REG_IBCR2 0xF9
  55. #define REG_IBISR0 0xFB
  56. #define COMMAND_TX_ENABLE 0x4
  57. #define COMMAND_RX_ENABLE 0x8
  58. #define COMMAND_RESET 0x10
  59. #define CPLUS_COMMAND_VERIFY_CHECKSUM 0x20
  60. #define CPLUS_COMMAND_VLAN_STRIP 0x40
  61. #define CPLUS_COMMAND_MAC_DBGO_SEL 0x1C
  62. #define CPLUS_COMMAND_PACKET_CONTROL_DISABLE 0x80
  63. #define CPLUS_COMMAND_ASF 0x100
  64. #define CPLUS_COMMAND_CXPL_DBG_SEL 0x200
  65. #define CPLUS_COMMAND_FORCE_TXFLOW_ENABLE 0x400
  66. #define CPLUS_COMMAND_FORCE_RXFLOW_ENABLE 0x800
  67. #define CPLUS_COMMAND_FORCE_HALF_DUP 0x1000
  68. #define CPLUS_COMMAND_MAC_DBGO_OE 0x4000
  69. #define CPLUS_COMMAND_ENABLE_BIST 0x8000
  70. #define INT_RXOK 0x1
  71. #define INT_RXERR 0x2
  72. #define INT_TXOK 0x4
  73. #define INT_TXERR 0x8
  74. #define INT_RX_OVERFLOW 0x10
  75. #define INT_LINK_CHANGE 0x20
  76. #define INT_RX_FIFO_OVERFLOW 0x40
  77. #define INT_SYS_ERR 0x8000
  78. #define CFG9346_NONE 0x00
  79. #define CFG9346_EEM0 0x40
  80. #define CFG9346_EEM1 0x80
  81. #define CFG9346_UNLOCK (CFG9346_EEM0 | CFG9346_EEM1)
  82. #define TXCFG_AUTO_FIFO 0x80
  83. #define TXCFG_MAX_DMA_UNLIMITED 0x700
  84. #define TXCFG_EMPTY 0x800
  85. #define TXCFG_IFG011 0x3000000
  86. #define RXCFG_READ_MASK 0x3F
  87. #define RXCFG_APM 0x2
  88. #define RXCFG_AM 0x4
  89. #define RXCFG_AB 0x8
  90. #define RXCFG_MAX_DMA_UNLIMITED 0x700
  91. #define RXCFG_EARLY_OFF_V2 0x800
  92. #define RXCFG_FTH_NONE 0xE000
  93. #define RXCFG_MULTI_ENABLE 0x4000
  94. #define RXCFG_128INT_ENABLE 0x8000
  95. #define CFG2_CLOCK_REQUEST_ENABLE 0x80
  96. #define CFG3_BEACON_ENABLE 0x1
  97. #define CFG3_READY_TO_L23 0x2
  98. #define CFG5_ASPM_ENABLE 0x1
  99. #define CFG5_SPI_ENABLE 0x8
  100. #define PHY_LINK_STATUS 0x2
  101. #define PHY_FLAG 0x80000000
  102. #define PHY_REG_BMCR 0x00
  103. #define PHY_REG_ANAR 0x4
  104. #define PHY_REG_GBCR 0x9
  105. #define CSI_FLAG 0x80000000
  106. #define CSI_BYTE_ENABLE 0xF000
  107. #define CSI_FUNC_NIC 0x20000
  108. #define CSI_FUNC_NIC2 0x10000
  109. #define CSI_ACCESS_1 0x17000000
  110. #define CSI_ACCESS_2 0x27000000
  111. #define EPHY_FLAG 0x80000000
  112. #define ERI_FLAG 0x80000000
  113. #define ERI_MASK_0001 0x1000
  114. #define ERI_MASK_0011 0x3000
  115. #define ERI_MASK_0100 0x4000
  116. #define ERI_MASK_0101 0x5000
  117. #define ERI_MASK_1111 0xF000
  118. #define ERI_EXGMAC 0x0
  119. #define OCP_FLAG 0x80000000
  120. #define OCP_STANDARD_PHY_BASE 0xa400
  121. #define TXSTART_START 0x40
  122. #define BMCR_RESET 0x8000
  123. #define BMCR_SPEED_0 0x2000
  124. #define BMCR_AUTO_NEGOTIATE 0x1000
  125. #define BMCR_RESTART_AUTO_NEGOTIATE 0x200
  126. #define BMCR_DUPLEX 0x100
  127. #define BMCR_SPEED_1 0x40
  128. #define ADVERTISE_10_HALF 0x20
  129. #define ADVERTISE_10_FULL 0x40
  130. #define ADVERTISE_100_HALF 0x80
  131. #define ADVERTISE_100_FULL 0x100
  132. #define ADVERTISE_PAUSE_CAP 0x400
  133. #define ADVERTISE_PAUSE_ASYM 0x800
  134. #define ADVERTISE_1000_HALF 0x100
  135. #define ADVERTISE_1000_FULL 0x200
  136. #define DLLPR_PFM_ENABLE 0x40
  137. #define DLLPR_TX_10M_PS_ENABLE 0x80
  138. #define MCU_LINK_LIST_READY 0x2
  139. #define MCU_RX_EMPTY 0x10
  140. #define MCU_TX_EMPTY 0x20
  141. #define MCU_NOW_IS_OOB 0x80
  142. #define MTPS_JUMBO 0x3F
  143. #define MISC_RXDV_GATE_ENABLE 0x80000
  144. #define MISC_PWM_ENABLE 0x400000
  145. #define MISC2_PFM_D3COLD_ENABLE 0x40
  146. #define PHYSTATUS_FULLDUP 0x01
  147. #define PHYSTATUS_1000MF 0x10
  148. #define PHYSTATUS_100M 0x08
  149. #define PHYSTATUS_10M 0x04
  150. #define TX_BUFFER_SIZE 0x1FF8
  151. #define RX_BUFFER_SIZE 0x1FF8 // FIXME: this should be increased (0x3FFF)
  152. UNMAP_AFTER_INIT ErrorOr<bool> RTL8168NetworkAdapter::probe(PCI::DeviceIdentifier const& pci_device_identifier)
  153. {
  154. if (pci_device_identifier.hardware_id().vendor_id != PCI::VendorID::Realtek)
  155. return false;
  156. if (pci_device_identifier.hardware_id().device_id != 0x8168)
  157. return false;
  158. return true;
  159. }
  160. UNMAP_AFTER_INIT ErrorOr<NonnullLockRefPtr<NetworkAdapter>> RTL8168NetworkAdapter::create(PCI::DeviceIdentifier const& pci_device_identifier)
  161. {
  162. u8 irq = pci_device_identifier.interrupt_line().value();
  163. auto interface_name = TRY(NetworkingManagement::generate_interface_name_from_pci_address(pci_device_identifier));
  164. auto registers_io_window = TRY(IOWindow::create_for_pci_device_bar(pci_device_identifier, PCI::HeaderType0BaseRegister::BAR0));
  165. return TRY(adopt_nonnull_lock_ref_or_enomem(new (nothrow) RTL8168NetworkAdapter(pci_device_identifier, irq, move(registers_io_window), move(interface_name))));
  166. }
  167. bool RTL8168NetworkAdapter::determine_supported_version() const
  168. {
  169. switch (m_version) {
  170. case ChipVersion::Version1:
  171. case ChipVersion::Version2:
  172. case ChipVersion::Version3:
  173. return true;
  174. case ChipVersion::Version4:
  175. case ChipVersion::Version5:
  176. case ChipVersion::Version6:
  177. case ChipVersion::Version7:
  178. case ChipVersion::Version8:
  179. case ChipVersion::Version9:
  180. case ChipVersion::Version10:
  181. case ChipVersion::Version11:
  182. case ChipVersion::Version12:
  183. case ChipVersion::Version13:
  184. case ChipVersion::Version14:
  185. return false;
  186. case ChipVersion::Version15:
  187. return true;
  188. case ChipVersion::Version16:
  189. return false;
  190. case ChipVersion::Version17:
  191. return true;
  192. case ChipVersion::Version18:
  193. case ChipVersion::Version19:
  194. case ChipVersion::Version20:
  195. case ChipVersion::Version21:
  196. case ChipVersion::Version22:
  197. case ChipVersion::Version23:
  198. case ChipVersion::Version24:
  199. case ChipVersion::Version25:
  200. case ChipVersion::Version26:
  201. case ChipVersion::Version27:
  202. case ChipVersion::Version28:
  203. case ChipVersion::Version29:
  204. return false;
  205. case ChipVersion::Version30:
  206. return true;
  207. default:
  208. return false;
  209. }
  210. }
  211. UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::DeviceIdentifier const& device_identifier, u8 irq, NonnullOwnPtr<IOWindow> registers_io_window, NonnullOwnPtr<KString> interface_name)
  212. : NetworkAdapter(move(interface_name))
  213. , PCI::Device(device_identifier)
  214. , IRQHandler(irq)
  215. , m_registers_io_window(move(registers_io_window))
  216. , m_rx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(TXDescriptor) * (number_of_rx_descriptors + 1)).release_value_but_fixme_should_propagate_errors(), "RTL8168 RX"sv, Memory::Region::Access::ReadWrite).release_value())
  217. , m_tx_descriptors_region(MM.allocate_contiguous_kernel_region(Memory::page_round_up(sizeof(RXDescriptor) * (number_of_tx_descriptors + 1)).release_value_but_fixme_should_propagate_errors(), "RTL8168 TX"sv, Memory::Region::Access::ReadWrite).release_value())
  218. {
  219. dmesgln_pci(*this, "Found @ {}", device_identifier.address());
  220. dmesgln_pci(*this, "I/O port base: {}", m_registers_io_window);
  221. }
  222. UNMAP_AFTER_INIT ErrorOr<void> RTL8168NetworkAdapter::initialize(Badge<NetworkingManagement>)
  223. {
  224. identify_chip_version();
  225. dmesgln_pci(*this, "Version detected - {} ({}{})", possible_device_name(), (u8)m_version, m_version_uncertain ? "?" : "");
  226. if (!determine_supported_version()) {
  227. dmesgln_pci(*this, "Aborting initialization! Support for your chip version ({}) is not implemented yet, please open a GH issue and include this message.", (u8)m_version);
  228. return Error::from_errno(ENODEV); // Each ChipVersion requires a specific implementation of configure_phy and hardware_quirks
  229. }
  230. // set initial REG_RXCFG
  231. auto rx_config = RXCFG_MAX_DMA_UNLIMITED;
  232. if (m_version <= ChipVersion::Version3) {
  233. rx_config |= RXCFG_FTH_NONE;
  234. } else if (m_version <= ChipVersion::Version8 || (m_version >= ChipVersion::Version16 && m_version <= ChipVersion::Version19)) {
  235. rx_config |= RXCFG_128INT_ENABLE | RXCFG_MULTI_ENABLE;
  236. } else if (m_version >= ChipVersion::Version21) {
  237. rx_config |= RXCFG_128INT_ENABLE | RXCFG_MULTI_ENABLE | RXCFG_EARLY_OFF_V2;
  238. } else {
  239. rx_config |= RXCFG_128INT_ENABLE;
  240. }
  241. out32(REG_RXCFG, rx_config);
  242. // disable interrupts
  243. out16(REG_IMR, 0);
  244. // initialize hardware
  245. if (m_version == ChipVersion::Version23 || m_version == ChipVersion::Version27 || m_version == ChipVersion::Version28) {
  246. // disable CMAC
  247. out8(REG_IBCR2, in8(REG_IBCR2) & ~1);
  248. while ((in32(REG_IBISR0) & 0x2) != 0)
  249. ;
  250. out8(REG_IBISR0, in8(REG_IBISR0) | 0x20);
  251. out8(REG_IBCR0, in8(REG_IBCR0) & ~1);
  252. }
  253. if (m_version >= ChipVersion::Version21) {
  254. m_ocp_base_address = OCP_STANDARD_PHY_BASE;
  255. // enable RXDV gate
  256. out32(REG_MISC, in32(REG_MISC) | MISC_RXDV_GATE_ENABLE);
  257. while ((in32(REG_TXCFG) & TXCFG_EMPTY) == 0)
  258. ;
  259. while ((in32(REG_MCU) & (MCU_RX_EMPTY | MCU_TX_EMPTY)) == 0)
  260. ;
  261. out8(REG_COMMAND, in8(REG_COMMAND) & ~(COMMAND_RX_ENABLE | COMMAND_TX_ENABLE));
  262. out8(REG_MCU, in8(REG_MCU) & ~MCU_NOW_IS_OOB);
  263. // vendor magic values ???
  264. auto data = ocp_in(0xe8de);
  265. data &= ~(1 << 14);
  266. ocp_out(0xe8de, data);
  267. while ((in32(REG_MCU) & MCU_LINK_LIST_READY) == 0)
  268. ;
  269. // vendor magic values ???
  270. data = ocp_in(0xe8de);
  271. data |= (1 << 15);
  272. ocp_out(0xe8de, data);
  273. while ((in32(REG_MCU) & MCU_LINK_LIST_READY) == 0)
  274. ;
  275. }
  276. // software reset
  277. reset();
  278. // clear interrupts
  279. out16(REG_ISR, 0xffff);
  280. enable_bus_mastering(device_identifier());
  281. read_mac_address();
  282. dmesgln_pci(*this, "MAC address: {}", mac_address().to_string());
  283. // notify about driver start
  284. if (m_version >= ChipVersion::Version11 && m_version <= ChipVersion::Version13) {
  285. // if check_dash
  286. // notify
  287. TODO();
  288. } else if (m_version == ChipVersion::Version23 || m_version == ChipVersion::Version27 || m_version == ChipVersion::Version28) {
  289. // if check_dash
  290. // notify
  291. TODO();
  292. }
  293. startup();
  294. return {};
  295. }
  296. void RTL8168NetworkAdapter::startup()
  297. {
  298. // initialize descriptors
  299. initialize_rx_descriptors();
  300. initialize_tx_descriptors();
  301. // register irq
  302. enable_irq();
  303. // version specific phy configuration
  304. configure_phy();
  305. // software reset phy
  306. phy_out(PHY_REG_BMCR, phy_in(PHY_REG_BMCR) | BMCR_RESET);
  307. while ((phy_in(PHY_REG_BMCR) & BMCR_RESET) != 0)
  308. ;
  309. set_phy_speed();
  310. // set C+ command
  311. auto cplus_command = in16(REG_CPLUS_COMMAND) | CPLUS_COMMAND_VERIFY_CHECKSUM | CPLUS_COMMAND_VLAN_STRIP;
  312. out16(REG_CPLUS_COMMAND, cplus_command);
  313. in16(REG_CPLUS_COMMAND); // C+ Command barrier
  314. // power up phy
  315. if (m_version >= ChipVersion::Version9 && m_version <= ChipVersion::Version15) {
  316. out8(REG_PMCH, in8(REG_PMCH) | 0x80);
  317. } else if (m_version >= ChipVersion::Version26) {
  318. out8(REG_PMCH, in8(REG_PMCH) | 0xC0);
  319. } else if (m_version >= ChipVersion::Version21 && m_version <= ChipVersion::Version23) {
  320. out8(REG_PMCH, in8(REG_PMCH) | 0xC0);
  321. // vendor magic values ???
  322. eri_update(0x1a8, ERI_MASK_1111, 0xfc000000, 0, ERI_EXGMAC);
  323. }
  324. // wakeup phy (more vendor magic values)
  325. phy_out(0x1F, 0);
  326. if (m_version <= ChipVersion::Version13) {
  327. phy_out(0x0E, 0);
  328. }
  329. phy_out(PHY_REG_BMCR, BMCR_AUTO_NEGOTIATE); // send known good phy write (acts as a phy barrier)
  330. start_hardware();
  331. // re-enable interrupts
  332. auto enabled_interrupts = INT_RXOK | INT_RXERR | INT_TXOK | INT_TXERR | INT_RX_OVERFLOW | INT_LINK_CHANGE | INT_SYS_ERR;
  333. if (m_version == ChipVersion::Version1) {
  334. enabled_interrupts |= INT_RX_FIFO_OVERFLOW;
  335. enabled_interrupts &= ~INT_RX_OVERFLOW;
  336. }
  337. out16(REG_IMR, enabled_interrupts);
  338. // update link status
  339. m_link_up = (in8(REG_PHYSTATUS) & PHY_LINK_STATUS) != 0;
  340. }
  341. void RTL8168NetworkAdapter::configure_phy()
  342. {
  343. // this method sets a bunch of magic vendor values to the phy configuration registers based on the hardware version
  344. switch (m_version) {
  345. case ChipVersion::Version1: {
  346. configure_phy_b_1();
  347. return;
  348. }
  349. case ChipVersion::Version2:
  350. case ChipVersion::Version3: {
  351. configure_phy_b_2();
  352. return;
  353. }
  354. case ChipVersion::Version4:
  355. TODO();
  356. case ChipVersion::Version5:
  357. TODO();
  358. case ChipVersion::Version6:
  359. TODO();
  360. case ChipVersion::Version7:
  361. TODO();
  362. case ChipVersion::Version8:
  363. TODO();
  364. case ChipVersion::Version9:
  365. TODO();
  366. case ChipVersion::Version10:
  367. TODO();
  368. case ChipVersion::Version11:
  369. TODO();
  370. case ChipVersion::Version12:
  371. TODO();
  372. case ChipVersion::Version13:
  373. TODO();
  374. case ChipVersion::Version14:
  375. TODO();
  376. case ChipVersion::Version15: {
  377. configure_phy_e_2();
  378. return;
  379. }
  380. case ChipVersion::Version16:
  381. TODO();
  382. case ChipVersion::Version17: {
  383. configure_phy_e_2();
  384. return;
  385. }
  386. case ChipVersion::Version18:
  387. TODO();
  388. case ChipVersion::Version19:
  389. TODO();
  390. case ChipVersion::Version20:
  391. TODO();
  392. case ChipVersion::Version21:
  393. TODO();
  394. case ChipVersion::Version22:
  395. TODO();
  396. case ChipVersion::Version23:
  397. TODO();
  398. case ChipVersion::Version24:
  399. TODO();
  400. case ChipVersion::Version25:
  401. TODO();
  402. case ChipVersion::Version26:
  403. TODO();
  404. case ChipVersion::Version27:
  405. TODO();
  406. case ChipVersion::Version28:
  407. TODO();
  408. case ChipVersion::Version29: {
  409. configure_phy_h_1();
  410. return;
  411. }
  412. case ChipVersion::Version30: {
  413. configure_phy_h_2();
  414. return;
  415. }
  416. default:
  417. VERIFY_NOT_REACHED();
  418. }
  419. }
  420. void RTL8168NetworkAdapter::configure_phy_b_1()
  421. {
  422. constexpr PhyRegister phy_registers[] = {
  423. { 0x10, 0xf41b },
  424. { 0x1f, 0 }
  425. };
  426. phy_out(0x1f, 0x1);
  427. phy_out(0x16, 1 << 0);
  428. phy_out_batch(phy_registers, 2);
  429. }
  430. void RTL8168NetworkAdapter::configure_phy_b_2()
  431. {
  432. constexpr PhyRegister phy_registers[] = {
  433. { 0x1f, 0x1 },
  434. { 0x10, 0xf41b },
  435. { 0x1f, 0 }
  436. };
  437. phy_out_batch(phy_registers, 3);
  438. }
  439. void RTL8168NetworkAdapter::configure_phy_e_2()
  440. {
  441. // FIXME: linux's driver writes a firmware blob to the device at this point, is this needed?
  442. constexpr PhyRegister phy_registers[] = {
  443. // Enable delay cap
  444. { 0x1f, 0x4 },
  445. { 0x1f, 0x7 },
  446. { 0x1e, 0xac },
  447. { 0x18, 0x6 },
  448. { 0x1f, 0x2 },
  449. { 0x1f, 0 },
  450. { 0x1f, 0 },
  451. // Channel estimation fine tune
  452. { 0x1f, 0x3 },
  453. { 0x9, 0xa20f },
  454. { 0x1f, 0 },
  455. { 0x1f, 0 },
  456. // Green Setting
  457. { 0x1f, 0x5 },
  458. { 0x5, 0x8b5b },
  459. { 0x6, 0x9222 },
  460. { 0x5, 0x8b6d },
  461. { 0x6, 0x8000 },
  462. { 0x5, 0x8b76 },
  463. { 0x6, 0x8000 },
  464. { 0x1f, 0 },
  465. };
  466. phy_out_batch(phy_registers, 19);
  467. // 4 corner performance improvement
  468. phy_out(0x1f, 0x5);
  469. phy_out(0x5, 0x8b80);
  470. phy_update(0x17, 0x6, 0);
  471. phy_out(0x1f, 0);
  472. // PHY auto speed down
  473. phy_out(0x1f, 0x4);
  474. phy_out(0x1f, 0x7);
  475. phy_out(0x1e, 0x2d);
  476. phy_update(0x18, 0x10, 0);
  477. phy_out(0x1f, 0x2);
  478. phy_out(0x1f, 0);
  479. phy_update(0x14, 0x8000, 0);
  480. // Improve 10M EEE waveform
  481. phy_out(0x1f, 0x5);
  482. phy_out(0x5, 0x8b86);
  483. phy_update(0x6, 0x1, 0);
  484. phy_out(0x1f, 0);
  485. // Improve 2-pair detection performance
  486. phy_out(0x1f, 0x5);
  487. phy_out(0x5, 0x8b85);
  488. phy_update(0x6, 0x4000, 0);
  489. phy_out(0x1f, 0);
  490. // EEE Setting
  491. eri_update(0x1b0, ERI_MASK_1111, 0, 0x3, ERI_EXGMAC);
  492. phy_out(0x1f, 0x5);
  493. phy_out(0x5, 0x8b85);
  494. phy_update(0x6, 0, 0x2000);
  495. phy_out(0x1f, 0x4);
  496. phy_out(0x1f, 0x7);
  497. phy_out(0x1e, 0x20);
  498. phy_update(0x15, 0, 0x100);
  499. phy_out(0x1f, 0x2);
  500. phy_out(0x1f, 0);
  501. phy_out(0xd, 0x7);
  502. phy_out(0xe, 0x3c);
  503. phy_out(0xd, 0x4007);
  504. phy_out(0xe, 0);
  505. phy_out(0xd, 0);
  506. // Green feature
  507. phy_out(0x1f, 0x3);
  508. phy_update(0x19, 0, 0x1);
  509. phy_update(0x10, 0, 0x400);
  510. phy_out(0x1f, 0);
  511. // Broken BIOS workaround: feed GigaMAC registers with MAC address.
  512. rar_exgmac_set();
  513. }
  514. void RTL8168NetworkAdapter::configure_phy_h_1()
  515. {
  516. // FIXME: linux's driver writes a firmware blob to the device at this point, is this needed?
  517. // CHN EST parameters adjust - giga master
  518. phy_out(0x1f, 0x0a43);
  519. phy_out(0x13, 0x809b);
  520. phy_update(0x14, 0x8000, 0xf800);
  521. phy_out(0x13, 0x80a2);
  522. phy_update(0x14, 0x8000, 0xff00);
  523. phy_out(0x13, 0x80a4);
  524. phy_update(0x14, 0x8500, 0xff00);
  525. phy_out(0x13, 0x809c);
  526. phy_update(0x14, 0xbd00, 0xff00);
  527. phy_out(0x1f, 0);
  528. // CHN EST parameters adjust - giga slave
  529. phy_out(0x1f, 0x0a43);
  530. phy_out(0x13, 0x80ad);
  531. phy_update(0x14, 0x7000, 0xf800);
  532. phy_out(0x13, 0x80b4);
  533. phy_update(0x14, 0x5000, 0xff00);
  534. phy_out(0x13, 0x80ac);
  535. phy_update(0x14, 0x4000, 0xff00);
  536. phy_out(0x1f, 0);
  537. // CHN EST parameters adjust - fnet
  538. phy_out(0x1f, 0x0a43);
  539. phy_out(0x13, 0x808e);
  540. phy_update(0x14, 0x1200, 0xff00);
  541. phy_out(0x13, 0x8090);
  542. phy_update(0x14, 0xe500, 0xff00);
  543. phy_out(0x13, 0x8092);
  544. phy_update(0x14, 0x9f00, 0xff00);
  545. phy_out(0x1f, 0);
  546. // enable R-tune & PGA-retune function
  547. u16 dout_tapbin = 0;
  548. phy_out(0x1f, 0x0a46);
  549. auto data = phy_in(0x13);
  550. data &= 3;
  551. data <<= 2;
  552. dout_tapbin |= data;
  553. data = phy_in(0x12);
  554. data &= 0xc000;
  555. data >>= 14;
  556. dout_tapbin |= data;
  557. dout_tapbin = ~(dout_tapbin ^ 0x8);
  558. dout_tapbin <<= 12;
  559. dout_tapbin &= 0xf000;
  560. phy_out(0x1f, 0x0a43);
  561. phy_out(0x13, 0x827a);
  562. phy_update(0x14, dout_tapbin, 0xf000);
  563. phy_out(0x13, 0x827b);
  564. phy_update(0x14, dout_tapbin, 0xf000);
  565. phy_out(0x13, 0x827c);
  566. phy_update(0x14, dout_tapbin, 0xf000);
  567. phy_out(0x13, 0x827d);
  568. phy_update(0x14, dout_tapbin, 0xf000);
  569. phy_out(0x1f, 0x0a43);
  570. phy_out(0x13, 0x811);
  571. phy_update(0x14, 0x800, 0);
  572. phy_out(0x1f, 0x0a42);
  573. phy_update(0x16, 0x2, 0);
  574. phy_out(0x1f, 0);
  575. // enable GPHY 10M
  576. phy_out(0x1f, 0x0a44);
  577. phy_update(0x11, 0x800, 0);
  578. phy_out(0x1f, 0);
  579. // SAR ADC performance
  580. phy_out(0x1f, 0x0bca);
  581. phy_update(0x17, 0x4000, 0x3000);
  582. phy_out(0x1f, 0);
  583. phy_out(0x1f, 0x0a43);
  584. phy_out(0x13, 0x803f);
  585. phy_update(0x14, 0, 0x3000);
  586. phy_out(0x13, 0x8047);
  587. phy_update(0x14, 0, 0x3000);
  588. phy_out(0x13, 0x804f);
  589. phy_update(0x14, 0, 0x3000);
  590. phy_out(0x13, 0x8057);
  591. phy_update(0x14, 0, 0x3000);
  592. phy_out(0x13, 0x805f);
  593. phy_update(0x14, 0, 0x3000);
  594. phy_out(0x13, 0x8067);
  595. phy_update(0x14, 0, 0x3000);
  596. phy_out(0x13, 0x806f);
  597. phy_update(0x14, 0, 0x3000);
  598. phy_out(0x1f, 0);
  599. // disable phy pfm mode
  600. phy_out(0x1f, 0x0a44);
  601. phy_update(0x11, 0, 0x80);
  602. phy_out(0x1f, 0);
  603. // Check ALDPS bit, disable it if enabled
  604. phy_out(0x1f, 0x0a43);
  605. if (phy_in(0x10) & 0x4)
  606. phy_update(0x10, 0, 0x4);
  607. phy_out(0x1f, 0);
  608. }
  609. void RTL8168NetworkAdapter::configure_phy_h_2()
  610. {
  611. // FIXME: linux's driver writes a firmware blob to the device at this point, is this needed?
  612. // CHIN EST parameter update
  613. phy_out(0x1f, 0x0a43);
  614. phy_out(0x13, 0x808a);
  615. phy_update(0x14, 0x000a, 0x3f);
  616. phy_out(0x1f, 0);
  617. // enable R-tune & PGA-retune function
  618. phy_out(0x1f, 0x0a43);
  619. phy_out(0x13, 0x811);
  620. phy_update(0x14, 0x800, 0);
  621. phy_out(0x1f, 0x0a42);
  622. phy_update(0x16, 0x2, 0);
  623. phy_out(0x1f, 0);
  624. // enable GPHY 10M
  625. phy_out(0x1f, 0x0a44);
  626. phy_update(0x11, 0x800, 0);
  627. phy_out(0x1f, 0);
  628. ocp_out(0xdd02, 0x807d);
  629. auto data = ocp_in(0xdd02);
  630. u16 ioffset_p3 = ((data & 0x80) >> 7);
  631. ioffset_p3 <<= 3;
  632. data = ocp_in(0xdd00);
  633. ioffset_p3 |= ((data & (0xe000)) >> 13);
  634. u16 ioffset_p2 = ((data & (0x1e00)) >> 9);
  635. u16 ioffset_p1 = ((data & (0x1e0)) >> 5);
  636. u16 ioffset_p0 = ((data & 0x10) >> 4);
  637. ioffset_p0 <<= 3;
  638. ioffset_p0 |= (data & (0x7));
  639. data = (ioffset_p3 << 12) | (ioffset_p2 << 8) | (ioffset_p1 << 4) | (ioffset_p0);
  640. if ((ioffset_p3 != 0x0f) || (ioffset_p2 != 0x0f) || (ioffset_p1 != 0x0f) || (ioffset_p0 != 0x0f)) {
  641. phy_out(0x1f, 0x0bcf);
  642. phy_out(0x16, data);
  643. phy_out(0x1f, 0);
  644. }
  645. // Modify rlen (TX LPF corner frequency) level
  646. phy_out(0x1f, 0x0bcd);
  647. data = phy_in(0x16);
  648. data &= 0x000f;
  649. u16 rlen = 0;
  650. if (data > 3)
  651. rlen = data - 3;
  652. data = rlen | (rlen << 4) | (rlen << 8) | (rlen << 12);
  653. phy_out(0x17, data);
  654. phy_out(0x1f, 0x0bcd);
  655. phy_out(0x1f, 0);
  656. // disable phy pfm mode
  657. phy_out(0x1f, 0x0a44);
  658. phy_update(0x11, 0, 0x80);
  659. phy_out(0x1f, 0);
  660. // Check ALDPS bit, disable it if enabled
  661. phy_out(0x1f, 0x0a43);
  662. if (phy_in(0x10) & 0x4)
  663. phy_update(0x10, 0, 0x4);
  664. phy_out(0x1f, 0);
  665. }
  666. void RTL8168NetworkAdapter::rar_exgmac_set()
  667. {
  668. auto mac = mac_address();
  669. const u16 w[] = {
  670. (u16)(mac[0] | (mac[1] << 8)),
  671. (u16)(mac[2] | (mac[3] << 8)),
  672. (u16)(mac[4] | (mac[5] << 8)),
  673. };
  674. const ExgMacRegister exg_mac_registers[] = {
  675. { 0xe0, ERI_MASK_1111, (u32)(w[0] | (w[1] << 16)) },
  676. { 0xe4, ERI_MASK_1111, (u32)w[2] },
  677. { 0xf0, ERI_MASK_1111, (u32)(w[0] << 16) },
  678. { 0xf4, ERI_MASK_1111, (u32)(w[1] | (w[2] << 16)) },
  679. };
  680. exgmac_out_batch(exg_mac_registers, 4);
  681. }
  682. void RTL8168NetworkAdapter::start_hardware()
  683. {
  684. // unlock config registers
  685. out8(REG_CFG9346, CFG9346_UNLOCK);
  686. // configure the maximum transmit packet size
  687. out16(REG_MTPS, MTPS_JUMBO);
  688. // configure the maximum receive packet size
  689. out16(REG_RMS, RX_BUFFER_SIZE);
  690. auto cplus_command = in16(REG_CPLUS_COMMAND);
  691. cplus_command |= CPLUS_COMMAND_PACKET_CONTROL_DISABLE;
  692. // undocumented magic value???
  693. cplus_command |= 0x1;
  694. out16(REG_CPLUS_COMMAND, cplus_command);
  695. // setup interrupt moderation, magic from vendor (Linux Driver uses 0x5151, *BSD Driver uses 0x5100, RTL Driver use 0x5f51???)
  696. out16(REG_INT_MOD, 0x5151);
  697. // point to tx descriptors
  698. out64(REG_TXADDR, m_tx_descriptors_region->physical_page(0)->paddr().get());
  699. // point to rx descriptors
  700. out64(REG_RXADDR, m_rx_descriptors_region->physical_page(0)->paddr().get());
  701. // configure tx: use the maximum dma transfer size, default interframe gap time.
  702. out32(REG_TXCFG, TXCFG_IFG011 | TXCFG_MAX_DMA_UNLIMITED);
  703. // version specific quirks and tweaks
  704. hardware_quirks();
  705. in8(REG_IMR); // known good read (acts as a barrier)
  706. // relock config registers
  707. out8(REG_CFG9346, CFG9346_NONE);
  708. // enable rx/tx
  709. out8(REG_COMMAND, COMMAND_RX_ENABLE | COMMAND_TX_ENABLE);
  710. // turn on all multicast
  711. out32(REG_MAR0, 0xFFFFFFFF);
  712. out32(REG_MAR4, 0xFFFFFFFF);
  713. // configure rx mode: accept physical (MAC) match, multicast, and broadcast
  714. out32(REG_RXCFG, (in32(REG_RXCFG) & ~RXCFG_READ_MASK) | RXCFG_APM | RXCFG_AM | RXCFG_AB);
  715. // disable early-rx interrupts
  716. out16(REG_MULTIINTR, in16(REG_MULTIINTR) & 0xF000);
  717. }
  718. void RTL8168NetworkAdapter::hardware_quirks()
  719. {
  720. switch (m_version) {
  721. case ChipVersion::Version1:
  722. hardware_quirks_b_1();
  723. return;
  724. case ChipVersion::Version2:
  725. case ChipVersion::Version3:
  726. hardware_quirks_b_2();
  727. return;
  728. case ChipVersion::Version4:
  729. TODO();
  730. case ChipVersion::Version5:
  731. TODO();
  732. case ChipVersion::Version6:
  733. TODO();
  734. case ChipVersion::Version7:
  735. TODO();
  736. case ChipVersion::Version8:
  737. TODO();
  738. case ChipVersion::Version9:
  739. TODO();
  740. case ChipVersion::Version10:
  741. TODO();
  742. case ChipVersion::Version11:
  743. TODO();
  744. case ChipVersion::Version12:
  745. TODO();
  746. case ChipVersion::Version13:
  747. TODO();
  748. case ChipVersion::Version14:
  749. TODO();
  750. case ChipVersion::Version15:
  751. return;
  752. case ChipVersion::Version16:
  753. TODO();
  754. case ChipVersion::Version17:
  755. hardware_quirks_e_2();
  756. return;
  757. case ChipVersion::Version18:
  758. TODO();
  759. case ChipVersion::Version19:
  760. TODO();
  761. case ChipVersion::Version20:
  762. TODO();
  763. case ChipVersion::Version21:
  764. TODO();
  765. case ChipVersion::Version22:
  766. TODO();
  767. case ChipVersion::Version23:
  768. TODO();
  769. case ChipVersion::Version24:
  770. TODO();
  771. case ChipVersion::Version25:
  772. TODO();
  773. case ChipVersion::Version26:
  774. TODO();
  775. case ChipVersion::Version27:
  776. TODO();
  777. case ChipVersion::Version28:
  778. TODO();
  779. case ChipVersion::Version29:
  780. case ChipVersion::Version30:
  781. hardware_quirks_h();
  782. return;
  783. default:
  784. VERIFY_NOT_REACHED();
  785. }
  786. }
  787. void RTL8168NetworkAdapter::hardware_quirks_b_1()
  788. {
  789. // disable checked reserved bits
  790. out8(REG_CONFIG3, in8(REG_CONFIG3) & ~CFG3_BEACON_ENABLE);
  791. constexpr u16 version1_cplus_quirks = CPLUS_COMMAND_ENABLE_BIST | CPLUS_COMMAND_MAC_DBGO_OE | CPLUS_COMMAND_FORCE_HALF_DUP | CPLUS_COMMAND_FORCE_RXFLOW_ENABLE | CPLUS_COMMAND_FORCE_TXFLOW_ENABLE | CPLUS_COMMAND_CXPL_DBG_SEL | CPLUS_COMMAND_ASF | CPLUS_COMMAND_PACKET_CONTROL_DISABLE | CPLUS_COMMAND_MAC_DBGO_SEL;
  792. out16(REG_CPLUS_COMMAND, in16(REG_CPLUS_COMMAND) & ~version1_cplus_quirks);
  793. }
  794. void RTL8168NetworkAdapter::hardware_quirks_b_2()
  795. {
  796. hardware_quirks_b_1();
  797. // configure the maximum transmit packet size (again)
  798. out16(REG_MTPS, MTPS_JUMBO);
  799. // disable checked reserved bits
  800. out8(REG_CONFIG4, in8(REG_CONFIG4) & ~1);
  801. }
  802. void RTL8168NetworkAdapter::hardware_quirks_e_2()
  803. {
  804. constexpr EPhyUpdate ephy_info[] = {
  805. { 0x9, 0, 0x80 },
  806. { 0x19, 0, 0x224 },
  807. };
  808. csi_enable(CSI_ACCESS_1);
  809. extended_phy_initialize(ephy_info, 2);
  810. // FIXME: MTU performance tweak
  811. eri_out(0xc0, ERI_MASK_0011, 0, ERI_EXGMAC);
  812. eri_out(0xb8, ERI_MASK_0011, 0, ERI_EXGMAC);
  813. eri_out(0xc8, ERI_MASK_1111, 0x100002, ERI_EXGMAC);
  814. eri_out(0xe8, ERI_MASK_1111, 0x100006, ERI_EXGMAC);
  815. eri_out(0xcc, ERI_MASK_1111, 0x50, ERI_EXGMAC);
  816. eri_out(0xd0, ERI_MASK_1111, 0x7ff0060, ERI_EXGMAC);
  817. eri_update(0x1b0, ERI_MASK_0001, 0x10, 0, ERI_EXGMAC);
  818. eri_update(0xd4, ERI_MASK_0011, 0xc00, 0xff00, ERI_EXGMAC);
  819. // Set early TX
  820. out8(REG_MTPS, 0x27);
  821. // FIXME: Disable PCIe clock request
  822. // enable tx auto fifo
  823. out32(REG_TXCFG, in32(REG_TXCFG) | TXCFG_AUTO_FIFO);
  824. out8(REG_MCU, in8(REG_MCU) & ~MCU_NOW_IS_OOB);
  825. // Set EEE LED frequency
  826. out8(REG_EEE_LED, in8(REG_EEE_LED) & ~0x7);
  827. out8(REG_DLLPR, in8(REG_DLLPR) | DLLPR_PFM_ENABLE);
  828. out32(REG_MISC, in32(REG_MISC) | MISC_PWM_ENABLE);
  829. out8(REG_CONFIG5, in8(REG_CONFIG5) & ~CFG5_SPI_ENABLE);
  830. }
  831. void RTL8168NetworkAdapter::hardware_quirks_h()
  832. {
  833. // disable aspm and clock request before accessing extended phy
  834. out8(REG_CONFIG2, in8(REG_CONFIG2) & ~CFG2_CLOCK_REQUEST_ENABLE);
  835. out8(REG_CONFIG5, in8(REG_CONFIG5) & ~CFG5_ASPM_ENABLE);
  836. // initialize extended phy
  837. constexpr EPhyUpdate ephy_info[] = {
  838. { 0x1e, 0x800, 0x1 },
  839. { 0x1d, 0, 0x800 },
  840. { 0x5, 0xffff, 0x2089 },
  841. { 0x6, 0xffff, 0x5881 },
  842. { 0x4, 0xffff, 0x154a },
  843. { 0x1, 0xffff, 0x68b }
  844. };
  845. extended_phy_initialize(ephy_info, 6);
  846. // enable tx auto fifo
  847. out32(REG_TXCFG, in32(REG_TXCFG) | TXCFG_AUTO_FIFO);
  848. // vendor magic values ???
  849. eri_out(0xC8, ERI_MASK_0101, 0x80002, ERI_EXGMAC);
  850. eri_out(0xCC, ERI_MASK_0001, 0x38, ERI_EXGMAC);
  851. eri_out(0xD0, ERI_MASK_0001, 0x48, ERI_EXGMAC);
  852. eri_out(0xE8, ERI_MASK_1111, 0x100006, ERI_EXGMAC);
  853. csi_enable(CSI_ACCESS_1);
  854. // vendor magic values ???
  855. eri_update(0xDC, ERI_MASK_0001, 0x0, 0x1, ERI_EXGMAC);
  856. eri_update(0xDC, ERI_MASK_0001, 0x1, 0x0, ERI_EXGMAC);
  857. eri_update(0xDC, ERI_MASK_1111, 0x10, 0x0, ERI_EXGMAC);
  858. eri_update(0xD4, ERI_MASK_1111, 0x1F00, 0x0, ERI_EXGMAC);
  859. eri_out(0x5F0, ERI_MASK_0011, 0x4F87, ERI_EXGMAC);
  860. // disable rxdv gate
  861. out32(REG_MISC, in32(REG_MISC) & ~MISC_RXDV_GATE_ENABLE);
  862. // set early TX
  863. out8(REG_MTPS, 0x27);
  864. // vendor magic values ???
  865. eri_out(0xC0, ERI_MASK_0011, 0, ERI_EXGMAC);
  866. eri_out(0xB8, ERI_MASK_0011, 0, ERI_EXGMAC);
  867. // Set EEE LED frequency
  868. out8(REG_EEE_LED, in8(REG_EEE_LED) & ~0x7);
  869. out8(REG_DLLPR, in8(REG_DLLPR) & ~DLLPR_PFM_ENABLE);
  870. out8(REG_MISC2, in8(REG_MISC2) & ~MISC2_PFM_D3COLD_ENABLE);
  871. out8(REG_DLLPR, in8(REG_DLLPR) & ~DLLPR_TX_10M_PS_ENABLE);
  872. // vendor magic values ???
  873. eri_update(0x1B0, ERI_MASK_0011, 0, 0x1000, ERI_EXGMAC);
  874. // disable l2l3 state
  875. out8(REG_CONFIG3, in8(REG_CONFIG3) & ~CFG3_READY_TO_L23);
  876. // blackmagic code taken from linux's r8169
  877. phy_out(0x1F, 0x0C42);
  878. auto rg_saw_count = (phy_in(0x13) & 0x3FFF);
  879. phy_out(0x1F, 0);
  880. if (rg_saw_count > 0) {
  881. u16 sw_count_1ms_ini = 16000000 / rg_saw_count;
  882. sw_count_1ms_ini &= 0x0fff;
  883. u32 data = ocp_in(0xd412);
  884. data &= ~0x0fff;
  885. data |= sw_count_1ms_ini;
  886. ocp_out(0xd412, data);
  887. }
  888. u32 data = ocp_in(0xe056);
  889. data &= ~0xf0;
  890. data |= 0x70;
  891. ocp_out(0xe056, data);
  892. data = ocp_in(0xe052);
  893. data &= ~0x6000;
  894. data |= 0x8008;
  895. ocp_out(0xe052, data);
  896. data = ocp_in(0xe0d6);
  897. data &= ~0x1ff;
  898. data |= 0x17f;
  899. ocp_out(0xe0d6, data);
  900. data = ocp_in(0xd420);
  901. data &= ~0x0fff;
  902. data |= 0x47f;
  903. ocp_out(0xd420, data);
  904. ocp_out(0xe63e, 0x1);
  905. ocp_out(0xe63e, 0);
  906. ocp_out(0xc094, 0);
  907. ocp_out(0xc09e, 0);
  908. }
  909. void RTL8168NetworkAdapter::set_phy_speed()
  910. {
  911. // wakeup phy
  912. phy_out(0x1F, 0);
  913. // advertise all available features to get best connection possible
  914. auto auto_negotiation_advertisement = phy_in(PHY_REG_ANAR);
  915. auto_negotiation_advertisement |= ADVERTISE_10_HALF; // 10 mbit half duplex
  916. auto_negotiation_advertisement |= ADVERTISE_10_FULL; // 10 mbit full duplex
  917. auto_negotiation_advertisement |= ADVERTISE_100_HALF; // 100 mbit half duplex
  918. auto_negotiation_advertisement |= ADVERTISE_100_FULL; // 100 mbit full duplex
  919. auto_negotiation_advertisement |= ADVERTISE_PAUSE_CAP; // capable of pause flow control
  920. auto_negotiation_advertisement |= ADVERTISE_PAUSE_ASYM; // capable of asymmetric pause flow control
  921. phy_out(PHY_REG_ANAR, auto_negotiation_advertisement);
  922. auto gigabyte_control = phy_in(PHY_REG_GBCR);
  923. gigabyte_control |= ADVERTISE_1000_HALF; // 1000 mbit half dulpex
  924. gigabyte_control |= ADVERTISE_1000_FULL; // 1000 mbit full duplex
  925. phy_out(PHY_REG_GBCR, gigabyte_control);
  926. // restart auto-negotiation with set advertisements
  927. phy_out(PHY_REG_BMCR, BMCR_AUTO_NEGOTIATE | BMCR_RESTART_AUTO_NEGOTIATE);
  928. }
  929. UNMAP_AFTER_INIT void RTL8168NetworkAdapter::initialize_rx_descriptors()
  930. {
  931. auto* rx_descriptors = (RXDescriptor*)m_rx_descriptors_region->vaddr().as_ptr();
  932. for (size_t i = 0; i < number_of_rx_descriptors; ++i) {
  933. auto& descriptor = rx_descriptors[i];
  934. auto region = MM.allocate_contiguous_kernel_region(Memory::page_round_up(RX_BUFFER_SIZE).release_value_but_fixme_should_propagate_errors(), "RTL8168 RX buffer"sv, Memory::Region::Access::ReadWrite).release_value();
  935. memset(region->vaddr().as_ptr(), 0, region->size()); // MM already zeros out newly allocated pages, but we do it again in case that ever changes
  936. m_rx_buffers_regions.append(move(region));
  937. descriptor.buffer_size = RX_BUFFER_SIZE;
  938. descriptor.flags = RXDescriptor::Ownership; // let the NIC know it can use this descriptor
  939. auto physical_address = m_rx_buffers_regions[i].physical_page(0)->paddr().get();
  940. descriptor.buffer_address_low = physical_address & 0xFFFFFFFF;
  941. descriptor.buffer_address_high = (u64)physical_address >> 32; // cast to prevent shift count >= with of type warnings in 32 bit systems
  942. }
  943. rx_descriptors[number_of_rx_descriptors - 1].flags = rx_descriptors[number_of_rx_descriptors - 1].flags | RXDescriptor::EndOfRing;
  944. }
  945. UNMAP_AFTER_INIT void RTL8168NetworkAdapter::initialize_tx_descriptors()
  946. {
  947. auto* tx_descriptors = (TXDescriptor*)m_tx_descriptors_region->vaddr().as_ptr();
  948. for (size_t i = 0; i < number_of_tx_descriptors; ++i) {
  949. auto& descriptor = tx_descriptors[i];
  950. auto region = MM.allocate_contiguous_kernel_region(Memory::page_round_up(TX_BUFFER_SIZE).release_value_but_fixme_should_propagate_errors(), "RTL8168 TX buffer"sv, Memory::Region::Access::ReadWrite).release_value();
  951. memset(region->vaddr().as_ptr(), 0, region->size()); // MM already zeros out newly allocated pages, but we do it again in case that ever changes
  952. m_tx_buffers_regions.append(move(region));
  953. descriptor.flags = TXDescriptor::FirstSegment | TXDescriptor::LastSegment;
  954. auto physical_address = m_tx_buffers_regions[i].physical_page(0)->paddr().get();
  955. descriptor.buffer_address_low = physical_address & 0xFFFFFFFF;
  956. descriptor.buffer_address_high = (u64)physical_address >> 32;
  957. }
  958. tx_descriptors[number_of_tx_descriptors - 1].flags = tx_descriptors[number_of_tx_descriptors - 1].flags | TXDescriptor::EndOfRing;
  959. }
  960. UNMAP_AFTER_INIT RTL8168NetworkAdapter::~RTL8168NetworkAdapter() = default;
  961. bool RTL8168NetworkAdapter::handle_irq(RegisterState const&)
  962. {
  963. bool was_handled = false;
  964. for (;;) {
  965. int status = in16(REG_ISR);
  966. out16(REG_ISR, status);
  967. m_entropy_source.add_random_event(status);
  968. dbgln_if(RTL8168_DEBUG, "RTL8168: handle_irq status={:#04x}", status);
  969. if ((status & (INT_RXOK | INT_RXERR | INT_TXOK | INT_TXERR | INT_RX_OVERFLOW | INT_LINK_CHANGE | INT_RX_FIFO_OVERFLOW | INT_SYS_ERR)) == 0)
  970. break;
  971. was_handled = true;
  972. if (status & INT_RXOK) {
  973. dbgln_if(RTL8168_DEBUG, "RTL8168: RX ready");
  974. receive();
  975. }
  976. if (status & INT_RXERR) {
  977. dbgln_if(RTL8168_DEBUG, "RTL8168: RX error - invalid packet");
  978. }
  979. if (status & INT_TXOK) {
  980. dbgln_if(RTL8168_DEBUG, "RTL8168: TX complete");
  981. m_wait_queue.wake_one();
  982. }
  983. if (status & INT_TXERR) {
  984. dbgln_if(RTL8168_DEBUG, "RTL8168: TX error - invalid packet");
  985. }
  986. if (status & INT_RX_OVERFLOW) {
  987. dmesgln_pci(*this, "RX descriptor unavailable (packet lost)");
  988. receive();
  989. }
  990. if (status & INT_LINK_CHANGE) {
  991. m_link_up = (in8(REG_PHYSTATUS) & PHY_LINK_STATUS) != 0;
  992. dmesgln_pci(*this, "Link status changed up={}", m_link_up);
  993. }
  994. if (status & INT_RX_FIFO_OVERFLOW) {
  995. dmesgln_pci(*this, "RX FIFO overflow");
  996. receive();
  997. }
  998. if (status & INT_SYS_ERR) {
  999. dmesgln_pci(*this, "Fatal system error");
  1000. }
  1001. }
  1002. return was_handled;
  1003. }
  1004. void RTL8168NetworkAdapter::reset()
  1005. {
  1006. out8(REG_COMMAND, COMMAND_RESET);
  1007. while ((in8(REG_COMMAND) & COMMAND_RESET) != 0)
  1008. ;
  1009. }
  1010. UNMAP_AFTER_INIT void RTL8168NetworkAdapter::read_mac_address()
  1011. {
  1012. MACAddress mac {};
  1013. for (int i = 0; i < 6; i++)
  1014. mac[i] = in8(REG_MAC + i);
  1015. set_mac_address(mac);
  1016. }
  1017. void RTL8168NetworkAdapter::send_raw(ReadonlyBytes payload)
  1018. {
  1019. dbgln_if(RTL8168_DEBUG, "RTL8168: send_raw length={}", payload.size());
  1020. if (payload.size() > TX_BUFFER_SIZE) {
  1021. dmesgln_pci(*this, "Packet was too big; discarding");
  1022. return;
  1023. }
  1024. auto* tx_descriptors = (TXDescriptor*)m_tx_descriptors_region->vaddr().as_ptr();
  1025. auto& free_descriptor = tx_descriptors[m_tx_free_index];
  1026. if ((free_descriptor.flags & TXDescriptor::Ownership) != 0) {
  1027. dbgln_if(RTL8168_DEBUG, "RTL8168: No free TX buffers, sleeping until one is available");
  1028. m_wait_queue.wait_forever("RTL8168NetworkAdapter"sv);
  1029. return send_raw(payload);
  1030. // if we woke up a TX descriptor is guaranteed to be available, so this should never recurse more than once
  1031. // but this can probably be done more cleanly
  1032. }
  1033. dbgln_if(RTL8168_DEBUG, "RTL8168: Chose descriptor {}", m_tx_free_index);
  1034. memcpy(m_tx_buffers_regions[m_tx_free_index].vaddr().as_ptr(), payload.data(), payload.size());
  1035. m_tx_free_index = (m_tx_free_index + 1) % number_of_tx_descriptors;
  1036. free_descriptor.frame_length = payload.size() & 0x3FFF;
  1037. free_descriptor.flags = free_descriptor.flags | TXDescriptor::Ownership;
  1038. out8(REG_TXSTART, TXSTART_START); // FIXME: this shouldn't be done so often, we should look into doing this using the watchdog timer
  1039. }
  1040. void RTL8168NetworkAdapter::receive()
  1041. {
  1042. auto* rx_descriptors = (RXDescriptor*)m_rx_descriptors_region->vaddr().as_ptr();
  1043. for (u16 i = 0; i < number_of_rx_descriptors; ++i) {
  1044. auto descriptor_index = (m_rx_free_index + i) % number_of_rx_descriptors;
  1045. auto& descriptor = rx_descriptors[descriptor_index];
  1046. if ((descriptor.flags & RXDescriptor::Ownership) != 0) {
  1047. m_rx_free_index = descriptor_index;
  1048. break;
  1049. }
  1050. u16 flags = descriptor.flags;
  1051. u16 length = descriptor.buffer_size & 0x3FFF;
  1052. dbgln_if(RTL8168_DEBUG, "RTL8168: receive, flags={:#04x}, length={}, descriptor={}", flags, length, descriptor_index);
  1053. if (length > RX_BUFFER_SIZE || (flags & RXDescriptor::ErrorSummary) != 0) {
  1054. dmesgln_pci(*this, "receive got bad packet, flags={:#04x}, length={}", flags, length);
  1055. } else if ((flags & RXDescriptor::FirstSegment) != 0 && (flags & RXDescriptor::LastSegment) == 0) {
  1056. VERIFY_NOT_REACHED();
  1057. // Our maximum received packet size is smaller than the descriptor buffer size, so packets should never be segmented
  1058. // if this happens on a real NIC it might not respect that, and we will have to support packet segmentation
  1059. } else {
  1060. did_receive({ m_rx_buffers_regions[descriptor_index].vaddr().as_ptr(), length });
  1061. }
  1062. descriptor.buffer_size = RX_BUFFER_SIZE;
  1063. flags = RXDescriptor::Ownership;
  1064. if (descriptor_index == number_of_rx_descriptors - 1)
  1065. flags |= RXDescriptor::EndOfRing;
  1066. descriptor.flags = flags; // let the NIC know it can use this descriptor again
  1067. }
  1068. }
  1069. void RTL8168NetworkAdapter::out8(u16 address, u8 data)
  1070. {
  1071. m_registers_io_window->write8(address, data);
  1072. }
  1073. void RTL8168NetworkAdapter::out16(u16 address, u16 data)
  1074. {
  1075. m_registers_io_window->write16(address, data);
  1076. }
  1077. void RTL8168NetworkAdapter::out32(u16 address, u32 data)
  1078. {
  1079. m_registers_io_window->write32(address, data);
  1080. }
  1081. void RTL8168NetworkAdapter::out64(u16 address, u64 data)
  1082. {
  1083. // ORDER MATTERS: Some NICs require the high part of the address to be written first
  1084. m_registers_io_window->write32(address + 4, (u32)(data >> 32));
  1085. m_registers_io_window->write32(address, (u32)(data & 0xFFFFFFFF));
  1086. }
  1087. u8 RTL8168NetworkAdapter::in8(u16 address)
  1088. {
  1089. return m_registers_io_window->read8(address);
  1090. }
  1091. u16 RTL8168NetworkAdapter::in16(u16 address)
  1092. {
  1093. return m_registers_io_window->read16(address);
  1094. }
  1095. u32 RTL8168NetworkAdapter::in32(u16 address)
  1096. {
  1097. return m_registers_io_window->read32(address);
  1098. }
  1099. void RTL8168NetworkAdapter::phy_out(u8 address, u16 data)
  1100. {
  1101. if (m_version == ChipVersion::Version11) {
  1102. TODO();
  1103. } else if (m_version == ChipVersion::Version12 || m_version == ChipVersion::Version13) {
  1104. TODO();
  1105. } else if (m_version >= ChipVersion::Version21) {
  1106. if (address == 0x1F) {
  1107. m_ocp_base_address = data ? data << 4 : OCP_STANDARD_PHY_BASE;
  1108. return;
  1109. }
  1110. if (m_ocp_base_address != OCP_STANDARD_PHY_BASE)
  1111. address -= 0x10;
  1112. ocp_phy_out(m_ocp_base_address + address * 2, data);
  1113. } else {
  1114. VERIFY((address & 0xE0) == 0); // register address is only 5 bit
  1115. out32(REG_PHYACCESS, PHY_FLAG | (address & 0x1F) << 16 | (data & 0xFFFF));
  1116. while ((in32(REG_PHYACCESS) & PHY_FLAG) != 0)
  1117. ;
  1118. }
  1119. }
  1120. u16 RTL8168NetworkAdapter::phy_in(u8 address)
  1121. {
  1122. if (m_version == ChipVersion::Version11) {
  1123. TODO();
  1124. } else if (m_version == ChipVersion::Version12 || m_version == ChipVersion::Version13) {
  1125. TODO();
  1126. } else if (m_version >= ChipVersion::Version21) {
  1127. if (m_ocp_base_address != OCP_STANDARD_PHY_BASE)
  1128. address -= 0x10;
  1129. return ocp_phy_in(m_ocp_base_address + address * 2);
  1130. } else {
  1131. VERIFY((address & 0xE0) == 0); // register address is only 5 bit
  1132. out32(REG_PHYACCESS, (address & 0x1F) << 16);
  1133. while ((in32(REG_PHYACCESS) & PHY_FLAG) == 0)
  1134. ;
  1135. return in32(REG_PHYACCESS) & 0xFFFF;
  1136. }
  1137. }
  1138. void RTL8168NetworkAdapter::phy_update(u32 address, u32 set, u32 clear)
  1139. {
  1140. auto value = phy_in(address);
  1141. phy_out(address, (value & ~clear) | set);
  1142. }
  1143. void RTL8168NetworkAdapter::phy_out_batch(const PhyRegister phy_registers[], size_t length)
  1144. {
  1145. for (size_t i = 0; i < length; i++) {
  1146. phy_out(phy_registers[i].address, phy_registers[i].data);
  1147. }
  1148. }
  1149. void RTL8168NetworkAdapter::extended_phy_out(u8 address, u16 data)
  1150. {
  1151. VERIFY((address & 0xE0) == 0); // register address is only 5 bit
  1152. out32(REG_EPHYACCESS, EPHY_FLAG | (address & 0x1F) << 16 | (data & 0xFFFF));
  1153. while ((in32(REG_EPHYACCESS) & EPHY_FLAG) != 0)
  1154. ;
  1155. }
  1156. u16 RTL8168NetworkAdapter::extended_phy_in(u8 address)
  1157. {
  1158. VERIFY((address & 0xE0) == 0); // register address is only 5 bit
  1159. out32(REG_EPHYACCESS, (address & 0x1F) << 16);
  1160. while ((in32(REG_EPHYACCESS) & EPHY_FLAG) == 0)
  1161. ;
  1162. return in32(REG_EPHYACCESS) & 0xFFFF;
  1163. }
  1164. void RTL8168NetworkAdapter::extended_phy_initialize(const EPhyUpdate ephy_info[], size_t length)
  1165. {
  1166. for (size_t i = 0; i < length; i++) {
  1167. auto updated_value = (extended_phy_in(ephy_info[i].offset) & ~ephy_info[i].clear) | ephy_info[i].set;
  1168. extended_phy_out(ephy_info[i].offset, updated_value);
  1169. }
  1170. }
  1171. void RTL8168NetworkAdapter::eri_out(u32 address, u32 mask, u32 data, u32 type)
  1172. {
  1173. out32(REG_ERI_DATA, data);
  1174. out32(REG_ERI_ADDR, ERI_FLAG | type | mask | address);
  1175. while ((in32(REG_ERI_ADDR) & ERI_FLAG) != 0)
  1176. ;
  1177. }
  1178. u32 RTL8168NetworkAdapter::eri_in(u32 address, u32 type)
  1179. {
  1180. out32(REG_ERI_ADDR, type | ERI_MASK_1111 | address);
  1181. while ((in32(REG_ERI_ADDR) & ERI_FLAG) == 0)
  1182. ;
  1183. return in32(REG_ERI_DATA);
  1184. }
  1185. void RTL8168NetworkAdapter::eri_update(u32 address, u32 mask, u32 set, u32 clear, u32 type)
  1186. {
  1187. auto value = eri_in(address, type);
  1188. eri_out(address, mask, (value & ~clear) | set, type);
  1189. }
  1190. void RTL8168NetworkAdapter::exgmac_out_batch(const ExgMacRegister exgmac_registers[], size_t length)
  1191. {
  1192. for (size_t i = 0; i < length; i++) {
  1193. eri_out(exgmac_registers[i].address, exgmac_registers[i].mask, exgmac_registers[i].value, ERI_EXGMAC);
  1194. }
  1195. }
  1196. void RTL8168NetworkAdapter::csi_out(u32 address, u32 data)
  1197. {
  1198. VERIFY(m_version >= ChipVersion::Version4);
  1199. out32(REG_CSI_DATA, data);
  1200. auto modifier = CSI_BYTE_ENABLE;
  1201. if (m_version == ChipVersion::Version20) {
  1202. modifier |= CSI_FUNC_NIC;
  1203. } else if (m_version == ChipVersion::Version26) {
  1204. modifier |= CSI_FUNC_NIC2;
  1205. }
  1206. out32(REG_CSI_ADDR, CSI_FLAG | (address & 0xFFF) | modifier);
  1207. while ((in32(REG_CSI_ADDR) & CSI_FLAG) != 0)
  1208. ;
  1209. }
  1210. u32 RTL8168NetworkAdapter::csi_in(u32 address)
  1211. {
  1212. VERIFY(m_version >= ChipVersion::Version4);
  1213. auto modifier = CSI_BYTE_ENABLE;
  1214. if (m_version == ChipVersion::Version20) {
  1215. modifier |= CSI_FUNC_NIC;
  1216. } else if (m_version == ChipVersion::Version26) {
  1217. modifier |= CSI_FUNC_NIC2;
  1218. }
  1219. out32(REG_CSI_ADDR, (address & 0xFFF) | modifier);
  1220. while ((in32(REG_CSI_ADDR) & CSI_FLAG) == 0)
  1221. ;
  1222. return in32(REG_CSI_DATA) & 0xFFFF;
  1223. }
  1224. void RTL8168NetworkAdapter::csi_enable(u32 bits)
  1225. {
  1226. auto csi = csi_in(0x70c) & 0x00ffffff;
  1227. csi_out(0x70c, csi | bits);
  1228. }
  1229. void RTL8168NetworkAdapter::ocp_out(u32 address, u32 data)
  1230. {
  1231. VERIFY((address & 0xFFFF0001) == 0);
  1232. out32(REG_OCP_DATA, OCP_FLAG | address << 15 | data);
  1233. }
  1234. u32 RTL8168NetworkAdapter::ocp_in(u32 address)
  1235. {
  1236. VERIFY((address & 0xFFFF0001) == 0);
  1237. out32(REG_OCP_DATA, address << 15);
  1238. return in32(REG_OCP_DATA);
  1239. }
  1240. void RTL8168NetworkAdapter::ocp_phy_out(u32 address, u32 data)
  1241. {
  1242. VERIFY((address & 0xFFFF0001) == 0);
  1243. out32(REG_GPHY_OCP, OCP_FLAG | (address << 15) | data);
  1244. while ((in32(REG_GPHY_OCP) & OCP_FLAG) != 0)
  1245. ;
  1246. }
  1247. u16 RTL8168NetworkAdapter::ocp_phy_in(u32 address)
  1248. {
  1249. VERIFY((address & 0xFFFF0001) == 0);
  1250. out32(REG_GPHY_OCP, address << 15);
  1251. while ((in32(REG_GPHY_OCP) & OCP_FLAG) == 0)
  1252. ;
  1253. return in32(REG_GPHY_OCP) & 0xFFFF;
  1254. }
  1255. void RTL8168NetworkAdapter::identify_chip_version()
  1256. {
  1257. auto transmit_config = in32(REG_TXCFG);
  1258. auto registers = transmit_config & 0x7c800000;
  1259. auto hw_version_id = transmit_config & 0x700000;
  1260. m_version_uncertain = false;
  1261. switch (registers) {
  1262. case 0x30000000:
  1263. m_version = ChipVersion::Version1;
  1264. break;
  1265. case 0x38000000:
  1266. if (hw_version_id == 00000) {
  1267. m_version = ChipVersion::Version2;
  1268. } else if (hw_version_id == 0x500000) {
  1269. m_version = ChipVersion::Version3;
  1270. } else {
  1271. m_version = ChipVersion::Version3;
  1272. m_version_uncertain = true;
  1273. }
  1274. break;
  1275. case 0x3C000000:
  1276. if (hw_version_id == 00000) {
  1277. m_version = ChipVersion::Version4;
  1278. } else if (hw_version_id == 0x200000) {
  1279. m_version = ChipVersion::Version5;
  1280. } else if (hw_version_id == 0x400000) {
  1281. m_version = ChipVersion::Version6;
  1282. } else {
  1283. m_version = ChipVersion::Version6;
  1284. m_version_uncertain = true;
  1285. }
  1286. break;
  1287. case 0x3C800000:
  1288. if (hw_version_id == 0x100000) {
  1289. m_version = ChipVersion::Version7;
  1290. } else if (hw_version_id == 0x300000) {
  1291. m_version = ChipVersion::Version8;
  1292. } else {
  1293. m_version = ChipVersion::Version8;
  1294. m_version_uncertain = true;
  1295. }
  1296. break;
  1297. case 0x28000000:
  1298. if (hw_version_id == 0x100000) {
  1299. m_version = ChipVersion::Version9;
  1300. } else if (hw_version_id == 0x300000) {
  1301. m_version = ChipVersion::Version10;
  1302. } else {
  1303. m_version = ChipVersion::Version10;
  1304. m_version_uncertain = true;
  1305. }
  1306. break;
  1307. case 0x28800000:
  1308. if (hw_version_id == 00000) {
  1309. m_version = ChipVersion::Version11;
  1310. } else if (hw_version_id == 0x200000) {
  1311. m_version = ChipVersion::Version12;
  1312. } else if (hw_version_id == 0x300000) {
  1313. m_version = ChipVersion::Version13;
  1314. } else {
  1315. m_version = ChipVersion::Version13;
  1316. m_version_uncertain = true;
  1317. }
  1318. break;
  1319. case 0x2C000000:
  1320. if (hw_version_id == 0x100000) {
  1321. m_version = ChipVersion::Version14;
  1322. } else if (hw_version_id == 0x200000) {
  1323. m_version = ChipVersion::Version15;
  1324. } else {
  1325. m_version = ChipVersion::Version15;
  1326. m_version_uncertain = true;
  1327. }
  1328. break;
  1329. case 0x2C800000:
  1330. if (hw_version_id == 00000) {
  1331. m_version = ChipVersion::Version16;
  1332. } else if (hw_version_id == 0x100000) {
  1333. m_version = ChipVersion::Version17;
  1334. } else {
  1335. m_version = ChipVersion::Version17;
  1336. m_version_uncertain = true;
  1337. }
  1338. break;
  1339. case 0x48000000:
  1340. if (hw_version_id == 00000) {
  1341. m_version = ChipVersion::Version18;
  1342. } else if (hw_version_id == 0x100000) {
  1343. m_version = ChipVersion::Version19;
  1344. } else {
  1345. m_version = ChipVersion::Version19;
  1346. m_version_uncertain = true;
  1347. }
  1348. break;
  1349. case 0x48800000:
  1350. if (hw_version_id == 00000) {
  1351. m_version = ChipVersion::Version20;
  1352. } else {
  1353. m_version = ChipVersion::Version20;
  1354. m_version_uncertain = true;
  1355. }
  1356. break;
  1357. case 0x4C000000:
  1358. if (hw_version_id == 00000) {
  1359. m_version = ChipVersion::Version21;
  1360. } else if (hw_version_id == 0x100000) {
  1361. m_version = ChipVersion::Version22;
  1362. } else {
  1363. m_version = ChipVersion::Version22;
  1364. m_version_uncertain = true;
  1365. }
  1366. break;
  1367. case 0x50000000:
  1368. if (hw_version_id == 00000) {
  1369. m_version = ChipVersion::Version23;
  1370. } else if (hw_version_id == 0x100000) {
  1371. m_version = ChipVersion::Version27;
  1372. } else if (hw_version_id == 0x200000) {
  1373. m_version = ChipVersion::Version28;
  1374. } else {
  1375. m_version = ChipVersion::Version28;
  1376. m_version_uncertain = true;
  1377. }
  1378. break;
  1379. case 0x50800000:
  1380. if (hw_version_id == 00000) {
  1381. m_version = ChipVersion::Version24;
  1382. } else if (hw_version_id == 0x100000) {
  1383. m_version = ChipVersion::Version25;
  1384. } else {
  1385. m_version = ChipVersion::Version25;
  1386. m_version_uncertain = true;
  1387. }
  1388. break;
  1389. case 0x5C800000:
  1390. if (hw_version_id == 00000) {
  1391. m_version = ChipVersion::Version26;
  1392. } else {
  1393. m_version = ChipVersion::Version26;
  1394. m_version_uncertain = true;
  1395. }
  1396. break;
  1397. case 0x54000000:
  1398. if (hw_version_id == 00000) {
  1399. m_version = ChipVersion::Version29;
  1400. } else if (hw_version_id == 0x100000) {
  1401. m_version = ChipVersion::Version30;
  1402. } else {
  1403. m_version = ChipVersion::Version30;
  1404. m_version_uncertain = true;
  1405. }
  1406. break;
  1407. default:
  1408. dbgln_if(RTL8168_DEBUG, "Unable to determine device version: {:#04x}", registers);
  1409. m_version = ChipVersion::Unknown;
  1410. m_version_uncertain = true;
  1411. break;
  1412. }
  1413. }
  1414. StringView RTL8168NetworkAdapter::possible_device_name()
  1415. {
  1416. switch (m_version) { // We are following *BSD's versioning scheme, the comments note linux's versioning scheme, but they dont match up exactly
  1417. case ChipVersion::Version1:
  1418. case ChipVersion::Version2:
  1419. case ChipVersion::Version3:
  1420. return "RTL8168B/8111B"sv; // 11, 12, 17
  1421. case ChipVersion::Version4:
  1422. case ChipVersion::Version5:
  1423. case ChipVersion::Version6:
  1424. return "RTL8168C/8111C"sv; // 19, 20, 21, 22
  1425. case ChipVersion::Version7:
  1426. case ChipVersion::Version8:
  1427. return "RTL8168CP/8111CP"sv; // 18, 23, 24
  1428. case ChipVersion::Version9:
  1429. case ChipVersion::Version10:
  1430. return "RTL8168D/8111D"sv; // 25, 26
  1431. case ChipVersion::Version11:
  1432. case ChipVersion::Version12:
  1433. case ChipVersion::Version13:
  1434. return "RTL8168DP/8111DP"sv; // 27, 28, 31
  1435. case ChipVersion::Version14:
  1436. case ChipVersion::Version15:
  1437. return "RTL8168E/8111E"sv; // 32, 33
  1438. case ChipVersion::Version16:
  1439. case ChipVersion::Version17:
  1440. return "RTL8168E-VL/8111E-VL"sv; // 34
  1441. case ChipVersion::Version18:
  1442. case ChipVersion::Version19:
  1443. return "RTL8168F/8111F"sv; // 35, 36
  1444. case ChipVersion::Version20:
  1445. return "RTL8411"sv; // 38
  1446. case ChipVersion::Version21:
  1447. case ChipVersion::Version22:
  1448. return "RTL8168G/8111G"sv; // 40, 41, 42
  1449. case ChipVersion::Version23:
  1450. case ChipVersion::Version27:
  1451. case ChipVersion::Version28:
  1452. return "RTL8168EP/8111EP"sv; // 49, 50, 51
  1453. case ChipVersion::Version24:
  1454. case ChipVersion::Version25:
  1455. return "RTL8168GU/8111GU"sv; // ???
  1456. case ChipVersion::Version26:
  1457. return "RTL8411B"sv; // 44
  1458. case ChipVersion::Version29:
  1459. case ChipVersion::Version30:
  1460. return "RTL8168H/8111H"sv; // 45, 46
  1461. case ChipVersion::Unknown:
  1462. return "Unknown"sv;
  1463. }
  1464. VERIFY_NOT_REACHED();
  1465. }
  1466. bool RTL8168NetworkAdapter::link_full_duplex()
  1467. {
  1468. u8 phystatus = in8(REG_PHYSTATUS);
  1469. return !!(phystatus & (PHYSTATUS_FULLDUP | PHYSTATUS_1000MF));
  1470. }
  1471. i32 RTL8168NetworkAdapter::link_speed()
  1472. {
  1473. if (!link_up())
  1474. return NetworkAdapter::LINKSPEED_INVALID;
  1475. u8 phystatus = in8(REG_PHYSTATUS);
  1476. if (phystatus & PHYSTATUS_1000MF)
  1477. return 1000;
  1478. if (phystatus & PHYSTATUS_100M)
  1479. return 100;
  1480. if (phystatus & PHYSTATUS_10M)
  1481. return 10;
  1482. return NetworkAdapter::LINKSPEED_INVALID;
  1483. }
  1484. }