RTL8139NetworkAdapter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/Debug.h>
  8. #include <Kernel/IO.h>
  9. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  10. #include <Kernel/Sections.h>
  11. namespace Kernel {
  12. #define REG_MAC 0x00
  13. #define REG_MAR0 0x08
  14. #define REG_MAR4 0x12
  15. #define REG_TXSTATUS0 0x10
  16. #define REG_TXADDR0 0x20
  17. #define REG_RXBUF 0x30
  18. #define REG_COMMAND 0x37
  19. #define REG_CAPR 0x38
  20. #define REG_IMR 0x3C
  21. #define REG_ISR 0x3E
  22. #define REG_TXCFG 0x40
  23. #define REG_RXCFG 0x44
  24. #define REG_MPC 0x4C
  25. #define REG_CFG9346 0x50
  26. #define REG_CONFIG1 0x52
  27. #define REG_MSR 0x58
  28. #define REG_BMCR 0x62
  29. #define REG_ANLPAR 0x68
  30. #define TX_STATUS_OWN 0x2000
  31. #define TX_STATUS_THRESHOLD_MAX 0x3F0000
  32. #define COMMAND_RX_EMPTY 0x01
  33. #define COMMAND_TX_ENABLE 0x04
  34. #define COMMAND_RX_ENABLE 0x08
  35. #define COMMAND_RESET 0x10
  36. #define INT_RXOK 0x01
  37. #define INT_RXERR 0x02
  38. #define INT_TXOK 0x04
  39. #define INT_TXERR 0x08
  40. #define INT_RX_BUFFER_OVERFLOW 0x10
  41. #define INT_LINK_CHANGE 0x20
  42. #define INT_RX_FIFO_OVERFLOW 0x40
  43. #define INT_LENGTH_CHANGE 0x2000
  44. #define INT_SYSTEM_ERROR 0x8000
  45. #define CFG9346_NONE 0x00
  46. #define CFG9346_EEM0 0x40
  47. #define CFG9346_EEM1 0x80
  48. #define TXCFG_TXRR_ZERO 0x00
  49. #define TXCFG_MAX_DMA_16B 0x000
  50. #define TXCFG_MAX_DMA_32B 0x100
  51. #define TXCFG_MAX_DMA_64B 0x200
  52. #define TXCFG_MAX_DMA_128B 0x300
  53. #define TXCFG_MAX_DMA_256B 0x400
  54. #define TXCFG_MAX_DMA_512B 0x500
  55. #define TXCFG_MAX_DMA_1K 0x600
  56. #define TXCFG_MAX_DMA_2K 0x700
  57. #define TXCFG_IFG11 0x3000000
  58. #define RXCFG_AAP 0x01
  59. #define RXCFG_APM 0x02
  60. #define RXCFG_AM 0x04
  61. #define RXCFG_AB 0x08
  62. #define RXCFG_AR 0x10
  63. #define RXCFG_WRAP_INHIBIT 0x80
  64. #define RXCFG_MAX_DMA_16B 0x000
  65. #define RXCFG_MAX_DMA_32B 0x100
  66. #define RXCFG_MAX_DMA_64B 0x200
  67. #define RXCFG_MAX_DMA_128B 0x300
  68. #define RXCFG_MAX_DMA_256B 0x400
  69. #define RXCFG_MAX_DMA_512B 0x500
  70. #define RXCFG_MAX_DMA_1K 0x600
  71. #define RXCFG_MAX_DMA_UNLIMITED 0x0700
  72. #define RXCFG_RBLN_8K 0x0000
  73. #define RXCFG_RBLN_16K 0x0800
  74. #define RXCFG_RBLN_32K 0x1000
  75. #define RXCFG_RBLN_64K 0x1800
  76. #define RXCFG_FTH_NONE 0xE000
  77. #define MSR_LINKB 0x02
  78. #define MSR_SPEED_10 0x08
  79. #define MSR_RX_FLOW_CONTROL_ENABLE 0x40
  80. #define BMCR_SPEED 0x2000
  81. #define BMCR_AUTO_NEGOTIATE 0x1000
  82. #define BMCR_DUPLEX 0x0100
  83. #define ANLPAR_10FD 0x0040
  84. #define ANLPAR_TXFD 0x0100
  85. #define RX_MULTICAST 0x8000
  86. #define RX_PHYSICAL_MATCH 0x4000
  87. #define RX_BROADCAST 0x2000
  88. #define RX_INVALID_SYMBOL_ERROR 0x20
  89. #define RX_RUNT 0x10
  90. #define RX_LONG 0x08
  91. #define RX_CRC_ERROR 0x04
  92. #define RX_FRAME_ALIGNMENT_ERROR 0x02
  93. #define RX_OK 0x01
  94. #define PACKET_SIZE_MAX 0x600
  95. #define PACKET_SIZE_MIN 0x16
  96. #define RX_BUFFER_SIZE 32768
  97. #define TX_BUFFER_SIZE PACKET_SIZE_MAX
  98. UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_initialize(PCI::Address address)
  99. {
  100. constexpr PCI::ID rtl8139_id = { 0x10EC, 0x8139 };
  101. auto id = PCI::get_id(address);
  102. if (id != rtl8139_id)
  103. return {};
  104. u8 irq = PCI::get_interrupt_line(address);
  105. return adopt_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(address, irq));
  106. }
  107. UNMAP_AFTER_INIT RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq)
  108. : PCI::Device(address)
  109. , IRQHandler(irq)
  110. , m_io_base(PCI::get_BAR0(pci_address()) & ~1)
  111. , m_rx_buffer(MM.allocate_contiguous_kernel_region(Memory::page_round_up(RX_BUFFER_SIZE + PACKET_SIZE_MAX), "RTL8139 RX", Memory::Region::Access::ReadWrite))
  112. , m_packet_buffer(MM.allocate_contiguous_kernel_region(Memory::page_round_up(PACKET_SIZE_MAX), "RTL8139 Packet buffer", Memory::Region::Access::ReadWrite))
  113. {
  114. m_tx_buffers.ensure_capacity(RTL8139_TX_BUFFER_COUNT);
  115. set_interface_name(address);
  116. dmesgln("RTL8139: Found @ {}", pci_address());
  117. enable_bus_mastering(pci_address());
  118. m_interrupt_line = PCI::get_interrupt_line(pci_address());
  119. dmesgln("RTL8139: I/O port base: {}", m_io_base);
  120. dmesgln("RTL8139: Interrupt line: {}", m_interrupt_line);
  121. // we add space to account for overhang from the last packet - the rtl8139
  122. // can optionally guarantee that packets will be contiguous by
  123. // purposefully overrunning the rx buffer
  124. dbgln("RTL8139: RX buffer: {}", m_rx_buffer->physical_page(0)->paddr());
  125. for (int i = 0; i < RTL8139_TX_BUFFER_COUNT; i++) {
  126. m_tx_buffers.append(MM.allocate_contiguous_kernel_region(Memory::page_round_up(TX_BUFFER_SIZE), "RTL8139 TX", Memory::Region::Access::Write | Memory::Region::Access::Read));
  127. dbgln("RTL8139: TX buffer {}: {}", i, m_tx_buffers[i]->physical_page(0)->paddr());
  128. }
  129. reset();
  130. read_mac_address();
  131. const auto& mac = mac_address();
  132. dmesgln("RTL8139: MAC address: {}", mac.to_string());
  133. enable_irq();
  134. }
  135. UNMAP_AFTER_INIT RTL8139NetworkAdapter::~RTL8139NetworkAdapter()
  136. {
  137. }
  138. bool RTL8139NetworkAdapter::handle_irq(const RegisterState&)
  139. {
  140. bool was_handled = false;
  141. for (;;) {
  142. int status = in16(REG_ISR);
  143. out16(REG_ISR, status);
  144. m_entropy_source.add_random_event(status);
  145. dbgln_if(RTL8139_DEBUG, "RTL8139: handle_irq status={:#04x}", status);
  146. if ((status & (INT_RXOK | INT_RXERR | INT_TXOK | INT_TXERR | INT_RX_BUFFER_OVERFLOW | INT_LINK_CHANGE | INT_RX_FIFO_OVERFLOW | INT_LENGTH_CHANGE | INT_SYSTEM_ERROR)) == 0)
  147. break;
  148. was_handled = true;
  149. if (status & INT_RXOK) {
  150. dbgln_if(RTL8139_DEBUG, "RTL8139: RX ready");
  151. receive();
  152. }
  153. if (status & INT_RXERR) {
  154. dmesgln("RTL8139: RX error - resetting device");
  155. reset();
  156. }
  157. if (status & INT_TXOK) {
  158. dbgln_if(RTL8139_DEBUG, "RTL8139: TX complete");
  159. }
  160. if (status & INT_TXERR) {
  161. dmesgln("RTL8139: TX error - resetting device");
  162. reset();
  163. }
  164. if (status & INT_RX_BUFFER_OVERFLOW) {
  165. dmesgln("RTL8139: RX buffer overflow");
  166. }
  167. if (status & INT_LINK_CHANGE) {
  168. m_link_up = (in8(REG_MSR) & MSR_LINKB) == 0;
  169. dmesgln("RTL8139: Link status changed up={}", m_link_up);
  170. }
  171. if (status & INT_RX_FIFO_OVERFLOW) {
  172. dmesgln("RTL8139: RX FIFO overflow");
  173. }
  174. if (status & INT_LENGTH_CHANGE) {
  175. dmesgln("RTL8139: Cable length change");
  176. }
  177. if (status & INT_SYSTEM_ERROR) {
  178. dmesgln("RTL8139: System error - resetting device");
  179. reset();
  180. }
  181. }
  182. return was_handled;
  183. }
  184. void RTL8139NetworkAdapter::reset()
  185. {
  186. m_rx_buffer_offset = 0;
  187. m_tx_next_buffer = 0;
  188. // reset the device to clear out all the buffers and config
  189. out8(REG_COMMAND, COMMAND_RESET);
  190. while ((in8(REG_COMMAND) & COMMAND_RESET) != 0)
  191. ;
  192. // unlock config registers
  193. out8(REG_CFG9346, CFG9346_EEM0 | CFG9346_EEM1);
  194. // turn on multicast
  195. out32(REG_MAR0, 0xffffffff);
  196. out32(REG_MAR4, 0xffffffff);
  197. // enable rx/tx
  198. out8(REG_COMMAND, COMMAND_RX_ENABLE | COMMAND_TX_ENABLE);
  199. // device might be in sleep mode, this will take it out
  200. out8(REG_CONFIG1, 0);
  201. // set up rx buffer
  202. out32(REG_RXBUF, m_rx_buffer->physical_page(0)->paddr().get());
  203. // reset missed packet counter
  204. out8(REG_MPC, 0);
  205. // "basic mode control register" options - 100mbit, full duplex, auto
  206. // negotiation
  207. out16(REG_BMCR, BMCR_SPEED | BMCR_AUTO_NEGOTIATE | BMCR_DUPLEX);
  208. // enable flow control
  209. out8(REG_MSR, MSR_RX_FLOW_CONTROL_ENABLE);
  210. // configure rx: accept physical (MAC) match, multicast, and broadcast,
  211. // use the optional contiguous packet feature, the maximum dma transfer
  212. // size, a 32k buffer, and no fifo threshold
  213. out32(REG_RXCFG, RXCFG_APM | RXCFG_AM | RXCFG_AB | RXCFG_WRAP_INHIBIT | RXCFG_MAX_DMA_UNLIMITED | RXCFG_RBLN_32K | RXCFG_FTH_NONE);
  214. // configure tx: default retry count (16), max DMA burst size of 1024
  215. // bytes, interframe gap time of the only allowable value. the DMA burst
  216. // size is important - silent failures have been observed with 2048 bytes.
  217. out32(REG_TXCFG, TXCFG_TXRR_ZERO | TXCFG_MAX_DMA_1K | TXCFG_IFG11);
  218. // tell the chip where we want it to DMA from for outgoing packets.
  219. for (int i = 0; i < 4; i++)
  220. out32(REG_TXADDR0 + (i * 4), m_tx_buffers[i]->physical_page(0)->paddr().get());
  221. // re-lock config registers
  222. out8(REG_CFG9346, CFG9346_NONE);
  223. // enable rx/tx again in case they got turned off (apparently some cards
  224. // do this?)
  225. out8(REG_COMMAND, COMMAND_RX_ENABLE | COMMAND_TX_ENABLE);
  226. // choose irqs, then clear any pending
  227. out16(REG_IMR, INT_RXOK | INT_RXERR | INT_TXOK | INT_TXERR | INT_RX_BUFFER_OVERFLOW | INT_LINK_CHANGE | INT_RX_FIFO_OVERFLOW | INT_LENGTH_CHANGE | INT_SYSTEM_ERROR);
  228. out16(REG_ISR, 0xffff);
  229. // Set the initial link up status.
  230. m_link_up = (in8(REG_MSR) & MSR_LINKB) == 0;
  231. }
  232. UNMAP_AFTER_INIT void RTL8139NetworkAdapter::read_mac_address()
  233. {
  234. MACAddress mac {};
  235. for (int i = 0; i < 6; i++)
  236. mac[i] = in8(REG_MAC + i);
  237. set_mac_address(mac);
  238. }
  239. void RTL8139NetworkAdapter::send_raw(ReadonlyBytes payload)
  240. {
  241. dbgln_if(RTL8139_DEBUG, "RTL8139: send_raw length={}", payload.size());
  242. if (payload.size() > PACKET_SIZE_MAX) {
  243. dmesgln("RTL8139: Packet was too big; discarding");
  244. return;
  245. }
  246. int hw_buffer = -1;
  247. for (int i = 0; i < RTL8139_TX_BUFFER_COUNT; i++) {
  248. int potential_buffer = (m_tx_next_buffer + i) % 4;
  249. auto status = in32(REG_TXSTATUS0 + (potential_buffer * 4));
  250. if (status & TX_STATUS_OWN) {
  251. hw_buffer = potential_buffer;
  252. break;
  253. }
  254. }
  255. if (hw_buffer == -1) {
  256. dmesgln("RTL8139: Hardware buffers full; discarding packet");
  257. return;
  258. }
  259. dbgln_if(RTL8139_DEBUG, "RTL8139: Chose buffer {}", hw_buffer);
  260. m_tx_next_buffer = (hw_buffer + 1) % 4;
  261. memcpy(m_tx_buffers[hw_buffer]->vaddr().as_ptr(), payload.data(), payload.size());
  262. memset(m_tx_buffers[hw_buffer]->vaddr().as_ptr() + payload.size(), 0, TX_BUFFER_SIZE - payload.size());
  263. // the rtl8139 will not actually emit packets onto the network if they're
  264. // smaller than 64 bytes. the rtl8139 adds a checksum to the end of each
  265. // packet, and that checksum is four bytes long, so we pad the packet to
  266. // 60 bytes if necessary to make sure the whole thing is large enough.
  267. auto length = payload.size();
  268. if (length < 60) {
  269. dbgln_if(RTL8139_DEBUG, "RTL8139: adjusting payload size from {} to 60", length);
  270. length = 60;
  271. }
  272. out32(REG_TXSTATUS0 + (hw_buffer * 4), length);
  273. }
  274. void RTL8139NetworkAdapter::receive()
  275. {
  276. auto* start_of_packet = m_rx_buffer->vaddr().as_ptr() + m_rx_buffer_offset;
  277. u16 status = *(const u16*)(start_of_packet + 0);
  278. u16 length = *(const u16*)(start_of_packet + 2);
  279. dbgln_if(RTL8139_DEBUG, "RTL8139: receive, status={:#04x}, length={}, offset={}", status, length, m_rx_buffer_offset);
  280. if (!(status & RX_OK) || (status & (RX_INVALID_SYMBOL_ERROR | RX_CRC_ERROR | RX_FRAME_ALIGNMENT_ERROR)) || (length >= PACKET_SIZE_MAX) || (length < PACKET_SIZE_MIN)) {
  281. dmesgln("RTL8139: receive got bad packet, status={:#04x}, length={}", status, length);
  282. reset();
  283. return;
  284. }
  285. // we never have to worry about the packet wrapping around the buffer,
  286. // since we set RXCFG_WRAP_INHIBIT, which allows the rtl8139 to write data
  287. // past the end of the allotted space.
  288. memcpy(m_packet_buffer->vaddr().as_ptr(), (const u8*)(start_of_packet + 4), length - 4);
  289. // let the card know that we've read this data
  290. m_rx_buffer_offset = ((m_rx_buffer_offset + length + 4 + 3) & ~3) % RX_BUFFER_SIZE;
  291. out16(REG_CAPR, m_rx_buffer_offset - 0x10);
  292. m_rx_buffer_offset %= RX_BUFFER_SIZE;
  293. did_receive({ m_packet_buffer->vaddr().as_ptr(), (size_t)(length - 4) });
  294. }
  295. void RTL8139NetworkAdapter::out8(u16 address, u8 data)
  296. {
  297. m_io_base.offset(address).out(data);
  298. }
  299. void RTL8139NetworkAdapter::out16(u16 address, u16 data)
  300. {
  301. m_io_base.offset(address).out(data);
  302. }
  303. void RTL8139NetworkAdapter::out32(u16 address, u32 data)
  304. {
  305. m_io_base.offset(address).out(data);
  306. }
  307. u8 RTL8139NetworkAdapter::in8(u16 address)
  308. {
  309. return m_io_base.offset(address).in<u8>();
  310. }
  311. u16 RTL8139NetworkAdapter::in16(u16 address)
  312. {
  313. return m_io_base.offset(address).in<u16>();
  314. }
  315. u32 RTL8139NetworkAdapter::in32(u16 address)
  316. {
  317. return m_io_base.offset(address).in<u32>();
  318. }
  319. bool RTL8139NetworkAdapter::link_full_duplex()
  320. {
  321. // Note: this code assumes auto-negotiation is enabled (which is now always the case) and
  322. // bases the duplex state on the link partner advertisement.
  323. // If non-auto-negotiation is ever implemented this should be changed.
  324. u16 anlpar = in16(REG_ANLPAR);
  325. return !!(anlpar & (ANLPAR_TXFD | ANLPAR_10FD));
  326. }
  327. i32 RTL8139NetworkAdapter::link_speed()
  328. {
  329. if (!link_up())
  330. return NetworkAdapter::LINKSPEED_INVALID;
  331. u16 msr = in16(REG_MSR);
  332. return msr & MSR_SPEED_10 ? 10 : 100;
  333. }
  334. }