RTL8139NetworkAdapter.cpp 13 KB

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