RTL8139NetworkAdapter.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/IO.h>
  27. #include <Kernel/Net/RTL8139NetworkAdapter.h>
  28. //#define RTL8139_DEBUG
  29. #define REG_MAC 0x00
  30. #define REG_MAR0 0x08
  31. #define REG_MAR4 0x12
  32. #define REG_TXSTATUS0 0x10
  33. #define REG_TXADDR0 0x20
  34. #define REG_RXBUF 0x30
  35. #define REG_COMMAND 0x37
  36. #define REG_CAPR 0x38
  37. #define REG_IMR 0x3C
  38. #define REG_ISR 0x3E
  39. #define REG_TXCFG 0x40
  40. #define REG_RXCFG 0x44
  41. #define REG_MPC 0x4C
  42. #define REG_CFG9346 0x50
  43. #define REG_CONFIG1 0x52
  44. #define REG_MSR 0x58
  45. #define REG_BMCR 0x62
  46. #define TX_STATUS_OWN 0x2000
  47. #define TX_STATUS_THRESHOLD_MAX 0x3F0000
  48. #define COMMAND_RX_EMPTY 0x01
  49. #define COMMAND_TX_ENABLE 0x04
  50. #define COMMAND_RX_ENABLE 0x08
  51. #define COMMAND_RESET 0x10
  52. #define INT_RXOK 0x01
  53. #define INT_RXERR 0x02
  54. #define INT_TXOK 0x04
  55. #define INT_TXERR 0x08
  56. #define INT_RX_BUFFER_OVERFLOW 0x10
  57. #define INT_LINK_CHANGE 0x20
  58. #define INT_RX_FIFO_OVERFLOW 0x40
  59. #define INT_LENGTH_CHANGE 0x2000
  60. #define INT_SYSTEM_ERROR 0x8000
  61. #define CFG9346_NONE 0x00
  62. #define CFG9346_EEM0 0x40
  63. #define CFG9346_EEM1 0x80
  64. #define TXCFG_TXRR_ZERO 0x00
  65. #define TXCFG_MAX_DMA_16B 0x000
  66. #define TXCFG_MAX_DMA_32B 0x100
  67. #define TXCFG_MAX_DMA_64B 0x200
  68. #define TXCFG_MAX_DMA_128B 0x300
  69. #define TXCFG_MAX_DMA_256B 0x400
  70. #define TXCFG_MAX_DMA_512B 0x500
  71. #define TXCFG_MAX_DMA_1K 0x600
  72. #define TXCFG_MAX_DMA_2K 0x700
  73. #define TXCFG_IFG11 0x3000000
  74. #define RXCFG_AAP 0x01
  75. #define RXCFG_APM 0x02
  76. #define RXCFG_AM 0x04
  77. #define RXCFG_AB 0x08
  78. #define RXCFG_AR 0x10
  79. #define RXCFG_WRAP_INHIBIT 0x80
  80. #define RXCFG_MAX_DMA_16B 0x000
  81. #define RXCFG_MAX_DMA_32B 0x100
  82. #define RXCFG_MAX_DMA_64B 0x200
  83. #define RXCFG_MAX_DMA_128B 0x300
  84. #define RXCFG_MAX_DMA_256B 0x400
  85. #define RXCFG_MAX_DMA_512B 0x500
  86. #define RXCFG_MAX_DMA_1K 0x600
  87. #define RXCFG_MAX_DMA_UNLIMITED 0x0700
  88. #define RXCFG_RBLN_8K 0x0000
  89. #define RXCFG_RBLN_16K 0x0800
  90. #define RXCFG_RBLN_32K 0x1000
  91. #define RXCFG_RBLN_64K 0x1800
  92. #define RXCFG_FTH_NONE 0xE000
  93. #define MSR_LINKB 0x02
  94. #define MSR_RX_FLOW_CONTROL_ENABLE 0x40
  95. #define BMCR_SPEED 0x2000
  96. #define BMCR_AUTO_NEGOTIATE 0x1000
  97. #define BMCR_DUPLEX 0x0100
  98. #define RX_MULTICAST 0x8000
  99. #define RX_PHYSICAL_MATCH 0x4000
  100. #define RX_BROADCAST 0x2000
  101. #define RX_INVALID_SYMBOL_ERROR 0x20
  102. #define RX_RUNT 0x10
  103. #define RX_LONG 0x08
  104. #define RX_CRC_ERROR 0x04
  105. #define RX_FRAME_ALIGNMENT_ERROR 0x02
  106. #define RX_OK 0x01
  107. #define PACKET_SIZE_MAX 0x600
  108. #define PACKET_SIZE_MIN 0x16
  109. #define RX_BUFFER_SIZE 32768
  110. #define TX_BUFFER_SIZE PACKET_SIZE_MAX
  111. void RTL8139NetworkAdapter::detect(const PCI::Address& address)
  112. {
  113. if (address.is_null())
  114. return;
  115. static const PCI::ID rtl8139_id = { 0x10EC, 0x8139 };
  116. PCI::ID id = PCI::get_id(address);
  117. if (id != rtl8139_id)
  118. return;
  119. u8 irq = PCI::get_interrupt_line(address);
  120. (void)adopt(*new RTL8139NetworkAdapter(address, irq)).leak_ref();
  121. }
  122. RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address pci_address, u8 irq)
  123. : IRQHandler(irq)
  124. , m_pci_address(pci_address)
  125. {
  126. set_interface_name("rtl8139");
  127. kprintf("RTL8139: Found at PCI address %b:%b:%b\n", pci_address.bus(), pci_address.slot(), pci_address.function());
  128. enable_bus_mastering(m_pci_address);
  129. m_io_base = PCI::get_BAR0(m_pci_address) & ~1;
  130. m_interrupt_line = PCI::get_interrupt_line(m_pci_address);
  131. kprintf("RTL8139: IO port base: %w\n", m_io_base);
  132. kprintf("RTL8139: Interrupt line: %u\n", m_interrupt_line);
  133. // we add space to account for overhang from the last packet - the rtl8139
  134. // can optionally guarantee that packets will be contiguous by
  135. // purposefully overrunning the rx buffer
  136. m_rx_buffer_addr = (uintptr_t)virtual_to_low_physical(kmalloc_aligned(RX_BUFFER_SIZE + PACKET_SIZE_MAX, 16));
  137. kprintf("RTL8139: RX buffer: P%p\n", m_rx_buffer_addr);
  138. auto tx_buffer_addr = (uintptr_t)virtual_to_low_physical(kmalloc_aligned(TX_BUFFER_SIZE * 4, 16));
  139. for (int i = 0; i < RTL8139_TX_BUFFER_COUNT; i++) {
  140. m_tx_buffer_addr[i] = tx_buffer_addr + TX_BUFFER_SIZE * i;
  141. kprintf("RTL8139: TX buffer %d: P%p\n", i, m_tx_buffer_addr[i]);
  142. }
  143. m_packet_buffer = (uintptr_t)kmalloc(PACKET_SIZE_MAX);
  144. reset();
  145. read_mac_address();
  146. const auto& mac = mac_address();
  147. kprintf("RTL8139: MAC address: %s\n", mac.to_string().characters());
  148. enable_irq();
  149. }
  150. RTL8139NetworkAdapter::~RTL8139NetworkAdapter()
  151. {
  152. }
  153. void RTL8139NetworkAdapter::handle_irq()
  154. {
  155. for (;;) {
  156. int status = in16(REG_ISR);
  157. out16(REG_ISR, status);
  158. #ifdef RTL8139_DEBUG
  159. kprintf("RTL8139NetworkAdapter::handle_irq status=%#04x\n", status);
  160. #endif
  161. 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)
  162. break;
  163. if (status & INT_RXOK) {
  164. #ifdef RTL8139_DEBUG
  165. kprintf("RTL8139NetworkAdapter: rx ready\n");
  166. #endif
  167. receive();
  168. }
  169. if (status & INT_RXERR) {
  170. kprintf("RTL8139NetworkAdapter: rx error - resetting device\n");
  171. reset();
  172. }
  173. if (status & INT_TXOK) {
  174. #ifdef RTL8139_DEBUG
  175. kprintf("RTL8139NetworkAdapter: tx complete\n");
  176. #endif
  177. }
  178. if (status & INT_TXERR) {
  179. kprintf("RTL8139NetworkAdapter: tx error - resetting device\n");
  180. reset();
  181. }
  182. if (status & INT_RX_BUFFER_OVERFLOW) {
  183. kprintf("RTL8139NetworkAdapter: rx buffer overflow\n");
  184. }
  185. if (status & INT_LINK_CHANGE) {
  186. m_link_up = (in8(REG_MSR) & MSR_LINKB) == 0;
  187. kprintf("RTL8139NetworkAdapter: link status changed up=%d\n", m_link_up);
  188. }
  189. if (status & INT_RX_FIFO_OVERFLOW) {
  190. kprintf("RTL8139NetworkAdapter: rx fifo overflow\n");
  191. }
  192. if (status & INT_LENGTH_CHANGE) {
  193. kprintf("RTL8139NetworkAdapter: cable length change\n");
  194. }
  195. if (status & INT_SYSTEM_ERROR) {
  196. kprintf("RTL8139NetworkAdapter: system error - resetting device\n");
  197. reset();
  198. }
  199. }
  200. }
  201. void RTL8139NetworkAdapter::reset()
  202. {
  203. m_rx_buffer_offset = 0;
  204. m_tx_next_buffer = 0;
  205. // reset the device to clear out all the buffers and config
  206. out8(REG_COMMAND, COMMAND_RESET);
  207. while ((in8(REG_COMMAND) & COMMAND_RESET) != 0)
  208. ;
  209. // unlock config registers
  210. out8(REG_CFG9346, CFG9346_EEM0 | CFG9346_EEM1);
  211. // turn on multicast
  212. out32(REG_MAR0, 0xffffffff);
  213. out32(REG_MAR4, 0xffffffff);
  214. // enable rx/tx
  215. out8(REG_COMMAND, COMMAND_RX_ENABLE | COMMAND_TX_ENABLE);
  216. // device might be in sleep mode, this will take it out
  217. out8(REG_CONFIG1, 0);
  218. // set up rx buffer
  219. out32(REG_RXBUF, m_rx_buffer_addr);
  220. // reset missed packet counter
  221. out8(REG_MPC, 0);
  222. // "basic mode control register" options - 100mbit, full duplex, auto
  223. // negotiation
  224. out16(REG_BMCR, BMCR_SPEED | BMCR_AUTO_NEGOTIATE | BMCR_DUPLEX);
  225. // enable flow control
  226. out8(REG_MSR, MSR_RX_FLOW_CONTROL_ENABLE);
  227. // configure rx: accept physical (MAC) match, multicast, and broadcast,
  228. // use the optional contiguous packet feature, the maximum dma transfer
  229. // size, a 32k buffer, and no fifo threshold
  230. out32(REG_RXCFG, RXCFG_APM | RXCFG_AM | RXCFG_AB | RXCFG_WRAP_INHIBIT | RXCFG_MAX_DMA_UNLIMITED | RXCFG_RBLN_32K | RXCFG_FTH_NONE);
  231. // configure tx: default retry count (16), max DMA burst size of 1024
  232. // bytes, interframe gap time of the only allowable value. the DMA burst
  233. // size is important - silent failures have been observed with 2048 bytes.
  234. out32(REG_TXCFG, TXCFG_TXRR_ZERO | TXCFG_MAX_DMA_1K | TXCFG_IFG11);
  235. // tell the chip where we want it to DMA from for outgoing packets.
  236. for (int i = 0; i < 4; i++)
  237. out32(REG_TXADDR0 + (i * 4), m_tx_buffer_addr[i]);
  238. // re-lock config registers
  239. out8(REG_CFG9346, CFG9346_NONE);
  240. // enable rx/tx again in case they got turned off (apparently some cards
  241. // do this?)
  242. out8(REG_COMMAND, COMMAND_RX_ENABLE | COMMAND_TX_ENABLE);
  243. // choose irqs, then clear any pending
  244. 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);
  245. out16(REG_ISR, 0xffff);
  246. }
  247. void RTL8139NetworkAdapter::read_mac_address()
  248. {
  249. u8 mac[6];
  250. for (int i = 0; i < 6; i++)
  251. mac[i] = in8(REG_MAC + i);
  252. set_mac_address(mac);
  253. }
  254. void RTL8139NetworkAdapter::send_raw(const u8* data, size_t length)
  255. {
  256. #ifdef RTL8139_DEBUG
  257. kprintf("RTL8139NetworkAdapter::send_raw length=%d\n", length);
  258. #endif
  259. if (length > PACKET_SIZE_MAX) {
  260. kprintf("RTL8139NetworkAdapter: packet was too big; discarding\n");
  261. return;
  262. }
  263. int hw_buffer = -1;
  264. for (int i = 0; i < RTL8139_TX_BUFFER_COUNT; i++) {
  265. int potential_buffer = (m_tx_next_buffer + i) % 4;
  266. auto status = in32(REG_TXSTATUS0 + (potential_buffer * 4));
  267. if (status & TX_STATUS_OWN) {
  268. hw_buffer = potential_buffer;
  269. break;
  270. }
  271. }
  272. if (hw_buffer == -1) {
  273. kprintf("RTL8139NetworkAdapter: hardware buffers full; discarding packet\n");
  274. return;
  275. } else {
  276. #ifdef RTL8139_DEBUG
  277. kprintf("RTL8139NetworkAdapter: chose buffer %d @ %p\n", hw_buffer, m_tx_buffer_addr[hw_buffer]);
  278. #endif
  279. m_tx_next_buffer = (hw_buffer + 1) % 4;
  280. }
  281. memcpy((void*)low_physical_to_virtual(m_tx_buffer_addr[hw_buffer]), data, length);
  282. memset((void*)(low_physical_to_virtual(m_tx_buffer_addr[hw_buffer]) + length), 0, TX_BUFFER_SIZE - length);
  283. // the rtl8139 will not actually emit packets onto the network if they're
  284. // smaller than 64 bytes. the rtl8139 adds a checksum to the end of each
  285. // packet, and that checksum is four bytes long, so we pad the packet to
  286. // 60 bytes if necessary to make sure the whole thing is large enough.
  287. if (length < 60) {
  288. #ifdef RTL8139_DEBUG
  289. kprintf("RTL8139NetworkAdapter: adjusting payload size from %zu to 60\n", length);
  290. #endif
  291. length = 60;
  292. }
  293. out32(REG_TXSTATUS0 + (hw_buffer * 4), length);
  294. }
  295. void RTL8139NetworkAdapter::receive()
  296. {
  297. auto* start_of_packet = (const u8*)(low_physical_to_virtual(m_rx_buffer_addr) + m_rx_buffer_offset);
  298. u16 status = *(const u16*)(start_of_packet + 0);
  299. u16 length = *(const u16*)(start_of_packet + 2);
  300. #ifdef RTL8139_DEBUG
  301. kprintf("RTL8139NetworkAdapter::receive status=%04x length=%d offset=%d\n", status, length, m_rx_buffer_offset);
  302. #endif
  303. if (!(status & RX_OK) || (status & (RX_INVALID_SYMBOL_ERROR | RX_CRC_ERROR | RX_FRAME_ALIGNMENT_ERROR)) || (length >= PACKET_SIZE_MAX) || (length < PACKET_SIZE_MIN)) {
  304. kprintf("RTL8139NetworkAdapter::receive got bad packet status=%04x length=%d\n", status, length);
  305. reset();
  306. return;
  307. }
  308. // we never have to worry about the packet wrapping around the buffer,
  309. // since we set RXCFG_WRAP_INHIBIT, which allows the rtl8139 to write data
  310. // past the end of the alloted space.
  311. memcpy((u8*)m_packet_buffer, (const u8*)(start_of_packet + 4), length - 4);
  312. // let the card know that we've read this data
  313. m_rx_buffer_offset = ((m_rx_buffer_offset + length + 4 + 3) & ~3) % RX_BUFFER_SIZE;
  314. out16(REG_CAPR, m_rx_buffer_offset - 0x10);
  315. m_rx_buffer_offset %= RX_BUFFER_SIZE;
  316. did_receive((const u8*)m_packet_buffer, length - 4);
  317. }
  318. void RTL8139NetworkAdapter::out8(u16 address, u8 data)
  319. {
  320. IO::out8(m_io_base + address, data);
  321. }
  322. void RTL8139NetworkAdapter::out16(u16 address, u16 data)
  323. {
  324. IO::out16(m_io_base + address, data);
  325. }
  326. void RTL8139NetworkAdapter::out32(u16 address, u32 data)
  327. {
  328. IO::out32(m_io_base + address, data);
  329. }
  330. u8 RTL8139NetworkAdapter::in8(u16 address)
  331. {
  332. return IO::in8(m_io_base + address);
  333. }
  334. u16 RTL8139NetworkAdapter::in16(u16 address)
  335. {
  336. return IO::in16(m_io_base + address);
  337. }
  338. u32 RTL8139NetworkAdapter::in32(u16 address)
  339. {
  340. return IO::in32(m_io_base + address);
  341. }