RTL8139NetworkAdapter.cpp 12 KB

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