E1000NetworkAdapter.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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/Net/E1000NetworkAdapter.h>
  27. #include <Kernel/Thread.h>
  28. #include <LibBareMetal/IO.h>
  29. //#define E1000_DEBUG
  30. namespace Kernel {
  31. #define REG_CTRL 0x0000
  32. #define REG_STATUS 0x0008
  33. #define REG_EEPROM 0x0014
  34. #define REG_CTRL_EXT 0x0018
  35. #define REG_IMASK 0x00D0
  36. #define REG_RCTRL 0x0100
  37. #define REG_RXDESCLO 0x2800
  38. #define REG_RXDESCHI 0x2804
  39. #define REG_RXDESCLEN 0x2808
  40. #define REG_RXDESCHEAD 0x2810
  41. #define REG_RXDESCTAIL 0x2818
  42. #define REG_TCTRL 0x0400
  43. #define REG_TXDESCLO 0x3800
  44. #define REG_TXDESCHI 0x3804
  45. #define REG_TXDESCLEN 0x3808
  46. #define REG_TXDESCHEAD 0x3810
  47. #define REG_TXDESCTAIL 0x3818
  48. #define REG_RDTR 0x2820 // RX Delay Timer Register
  49. #define REG_RXDCTL 0x3828 // RX Descriptor Control
  50. #define REG_RADV 0x282C // RX Int. Absolute Delay Timer
  51. #define REG_RSRPD 0x2C00 // RX Small Packet Detect Interrupt
  52. #define REG_TIPG 0x0410 // Transmit Inter Packet Gap
  53. #define ECTRL_SLU 0x40 //set link up
  54. #define RCTL_EN (1 << 1) // Receiver Enable
  55. #define RCTL_SBP (1 << 2) // Store Bad Packets
  56. #define RCTL_UPE (1 << 3) // Unicast Promiscuous Enabled
  57. #define RCTL_MPE (1 << 4) // Multicast Promiscuous Enabled
  58. #define RCTL_LPE (1 << 5) // Long Packet Reception Enable
  59. #define RCTL_LBM_NONE (0 << 6) // No Loopback
  60. #define RCTL_LBM_PHY (3 << 6) // PHY or external SerDesc loopback
  61. #define RTCL_RDMTS_HALF (0 << 8) // Free Buffer Threshold is 1/2 of RDLEN
  62. #define RTCL_RDMTS_QUARTER (1 << 8) // Free Buffer Threshold is 1/4 of RDLEN
  63. #define RTCL_RDMTS_EIGHTH (2 << 8) // Free Buffer Threshold is 1/8 of RDLEN
  64. #define RCTL_MO_36 (0 << 12) // Multicast Offset - bits 47:36
  65. #define RCTL_MO_35 (1 << 12) // Multicast Offset - bits 46:35
  66. #define RCTL_MO_34 (2 << 12) // Multicast Offset - bits 45:34
  67. #define RCTL_MO_32 (3 << 12) // Multicast Offset - bits 43:32
  68. #define RCTL_BAM (1 << 15) // Broadcast Accept Mode
  69. #define RCTL_VFE (1 << 18) // VLAN Filter Enable
  70. #define RCTL_CFIEN (1 << 19) // Canonical Form Indicator Enable
  71. #define RCTL_CFI (1 << 20) // Canonical Form Indicator Bit Value
  72. #define RCTL_DPF (1 << 22) // Discard Pause Frames
  73. #define RCTL_PMCF (1 << 23) // Pass MAC Control Frames
  74. #define RCTL_SECRC (1 << 26) // Strip Ethernet CRC
  75. // Buffer Sizes
  76. #define RCTL_BSIZE_256 (3 << 16)
  77. #define RCTL_BSIZE_512 (2 << 16)
  78. #define RCTL_BSIZE_1024 (1 << 16)
  79. #define RCTL_BSIZE_2048 (0 << 16)
  80. #define RCTL_BSIZE_4096 ((3 << 16) | (1 << 25))
  81. #define RCTL_BSIZE_8192 ((2 << 16) | (1 << 25))
  82. #define RCTL_BSIZE_16384 ((1 << 16) | (1 << 25))
  83. // Transmit Command
  84. #define CMD_EOP (1 << 0) // End of Packet
  85. #define CMD_IFCS (1 << 1) // Insert FCS
  86. #define CMD_IC (1 << 2) // Insert Checksum
  87. #define CMD_RS (1 << 3) // Report Status
  88. #define CMD_RPS (1 << 4) // Report Packet Sent
  89. #define CMD_VLE (1 << 6) // VLAN Packet Enable
  90. #define CMD_IDE (1 << 7) // Interrupt Delay Enable
  91. // TCTL Register
  92. #define TCTL_EN (1 << 1) // Transmit Enable
  93. #define TCTL_PSP (1 << 3) // Pad Short Packets
  94. #define TCTL_CT_SHIFT 4 // Collision Threshold
  95. #define TCTL_COLD_SHIFT 12 // Collision Distance
  96. #define TCTL_SWXOFF (1 << 22) // Software XOFF Transmission
  97. #define TCTL_RTLC (1 << 24) // Re-transmit on Late Collision
  98. #define TSTA_DD (1 << 0) // Descriptor Done
  99. #define TSTA_EC (1 << 1) // Excess Collisions
  100. #define TSTA_LC (1 << 2) // Late Collision
  101. #define LSTA_TU (1 << 3) // Transmit Underrun
  102. // STATUS Register
  103. #define STATUS_FD 0x01
  104. #define STATUS_LU 0x02
  105. #define STATUS_TXOFF 0x08
  106. #define STATUS_SPEED 0xC0
  107. #define STATUS_SPEED_10MB 0x00
  108. #define STATUS_SPEED_100MB 0x40
  109. #define STATUS_SPEED_1000MB1 0x80
  110. #define STATUS_SPEED_1000MB2 0xC0
  111. void E1000NetworkAdapter::detect(const PCI::Address& address)
  112. {
  113. if (address.is_null())
  114. return;
  115. static const PCI::ID qemu_bochs_vbox_id = { 0x8086, 0x100e };
  116. const PCI::ID id = PCI::get_id(address);
  117. if (id != qemu_bochs_vbox_id)
  118. return;
  119. u8 irq = PCI::get_interrupt_line(address);
  120. (void)adopt(*new E1000NetworkAdapter(address, irq)).leak_ref();
  121. }
  122. E1000NetworkAdapter::E1000NetworkAdapter(PCI::Address address, u8 irq)
  123. : PCI::Device(address, irq)
  124. {
  125. set_interface_name("e1k");
  126. kprintf("E1000: Found at PCI address @ %w:%b:%b.%b\n", pci_address().seg(), pci_address().bus(), pci_address().slot(), pci_address().function());
  127. enable_bus_mastering(pci_address());
  128. size_t mmio_base_size = PCI::get_BAR_Space_Size(pci_address(), 0);
  129. m_mmio_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of(PCI::get_BAR0(pci_address()))), PAGE_ROUND_UP(mmio_base_size), "E1000 MMIO", Region::Access::Read | Region::Access::Write, false, false);
  130. m_mmio_base = m_mmio_region->vaddr();
  131. m_use_mmio = true;
  132. m_io_base = PCI::get_BAR1(pci_address()) & ~1;
  133. m_interrupt_line = PCI::get_interrupt_line(pci_address());
  134. kprintf("E1000: IO port base: %w\n", m_io_base);
  135. kprintf("E1000: MMIO base: P%x\n", PCI::get_BAR0(pci_address()) & 0xfffffffc);
  136. kprintf("E1000: MMIO base size: %u bytes\n", mmio_base_size);
  137. kprintf("E1000: Interrupt line: %u\n", m_interrupt_line);
  138. detect_eeprom();
  139. kprintf("E1000: Has EEPROM? %u\n", m_has_eeprom);
  140. read_mac_address();
  141. const auto& mac = mac_address();
  142. kprintf("E1000: MAC address: %b:%b:%b:%b:%b:%b\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
  143. u32 flags = in32(REG_CTRL);
  144. out32(REG_CTRL, flags | ECTRL_SLU);
  145. initialize_rx_descriptors();
  146. initialize_tx_descriptors();
  147. out32(REG_IMASK, 0x1f6dc);
  148. out32(REG_IMASK, 0xff & ~4);
  149. in32(0xc0);
  150. enable_irq();
  151. }
  152. E1000NetworkAdapter::~E1000NetworkAdapter()
  153. {
  154. }
  155. void E1000NetworkAdapter::handle_irq(RegisterState&)
  156. {
  157. out32(REG_IMASK, 0x1);
  158. u32 status = in32(0xc0);
  159. if (status & 4) {
  160. u32 flags = in32(REG_CTRL);
  161. out32(REG_CTRL, flags | ECTRL_SLU);
  162. }
  163. if (status & 0x10) {
  164. // Threshold OK?
  165. }
  166. if (status & 0x80) {
  167. receive();
  168. }
  169. m_wait_queue.wake_all();
  170. }
  171. void E1000NetworkAdapter::detect_eeprom()
  172. {
  173. out32(REG_EEPROM, 0x1);
  174. for (volatile int i = 0; i < 999; ++i) {
  175. u32 data = in32(REG_EEPROM);
  176. if (data & 0x10) {
  177. m_has_eeprom = true;
  178. return;
  179. }
  180. }
  181. m_has_eeprom = false;
  182. }
  183. u32 E1000NetworkAdapter::read_eeprom(u8 address)
  184. {
  185. u16 data = 0;
  186. u32 tmp = 0;
  187. if (m_has_eeprom) {
  188. out32(REG_EEPROM, ((u32)address << 8) | 1);
  189. while (!((tmp = in32(REG_EEPROM)) & (1 << 4)))
  190. ;
  191. } else {
  192. out32(REG_EEPROM, ((u32)address << 2) | 1);
  193. while (!((tmp = in32(REG_EEPROM)) & (1 << 1)))
  194. ;
  195. }
  196. data = (tmp >> 16) & 0xffff;
  197. return data;
  198. }
  199. void E1000NetworkAdapter::read_mac_address()
  200. {
  201. if (m_has_eeprom) {
  202. u8 mac[6];
  203. u32 tmp = read_eeprom(0);
  204. mac[0] = tmp & 0xff;
  205. mac[1] = tmp >> 8;
  206. tmp = read_eeprom(1);
  207. mac[2] = tmp & 0xff;
  208. mac[3] = tmp >> 8;
  209. tmp = read_eeprom(2);
  210. mac[4] = tmp & 0xff;
  211. mac[5] = tmp >> 8;
  212. set_mac_address(mac);
  213. } else {
  214. ASSERT_NOT_REACHED();
  215. }
  216. }
  217. bool E1000NetworkAdapter::link_up()
  218. {
  219. return (in32(REG_STATUS) & STATUS_LU);
  220. }
  221. void E1000NetworkAdapter::initialize_rx_descriptors()
  222. {
  223. auto ptr = (uintptr_t)kmalloc_eternal(sizeof(e1000_rx_desc) * number_of_rx_descriptors + 16);
  224. // Make sure it's 16-byte aligned.
  225. if (ptr % 16)
  226. ptr = (ptr + 16) - (ptr % 16);
  227. m_rx_descriptors = (e1000_rx_desc*)ptr;
  228. for (int i = 0; i < number_of_rx_descriptors; ++i) {
  229. auto& descriptor = m_rx_descriptors[i];
  230. auto addr = (uintptr_t)kmalloc_eternal(8192 + 16);
  231. if (addr % 16)
  232. addr = (addr + 16) - (addr % 16);
  233. descriptor.addr = addr - 0xc0000000;
  234. descriptor.status = 0;
  235. }
  236. out32(REG_RXDESCLO, (u32)ptr - 0xc0000000);
  237. out32(REG_RXDESCHI, 0);
  238. out32(REG_RXDESCLEN, number_of_rx_descriptors * sizeof(e1000_rx_desc));
  239. out32(REG_RXDESCHEAD, 0);
  240. out32(REG_RXDESCTAIL, number_of_rx_descriptors - 1);
  241. out32(REG_RCTRL, RCTL_EN | RCTL_SBP | RCTL_UPE | RCTL_MPE | RCTL_LBM_NONE | RTCL_RDMTS_HALF | RCTL_BAM | RCTL_SECRC | RCTL_BSIZE_8192);
  242. }
  243. void E1000NetworkAdapter::initialize_tx_descriptors()
  244. {
  245. auto ptr = (uintptr_t)kmalloc_eternal(sizeof(e1000_tx_desc) * number_of_tx_descriptors + 16);
  246. // Make sure it's 16-byte aligned.
  247. if (ptr % 16)
  248. ptr = (ptr + 16) - (ptr % 16);
  249. m_tx_descriptors = (e1000_tx_desc*)ptr;
  250. for (int i = 0; i < number_of_tx_descriptors; ++i) {
  251. auto& descriptor = m_tx_descriptors[i];
  252. auto addr = (uintptr_t)kmalloc_eternal(8192 + 16);
  253. if (addr % 16)
  254. addr = (addr + 16) - (addr % 16);
  255. descriptor.addr = addr - 0xc0000000;
  256. descriptor.cmd = 0;
  257. }
  258. out32(REG_TXDESCLO, (u32)ptr - 0xc0000000);
  259. out32(REG_TXDESCHI, 0);
  260. out32(REG_TXDESCLEN, number_of_tx_descriptors * sizeof(e1000_tx_desc));
  261. out32(REG_TXDESCHEAD, 0);
  262. out32(REG_TXDESCTAIL, 0);
  263. out32(REG_TCTRL, in32(REG_TCTRL) | TCTL_EN | TCTL_PSP);
  264. out32(REG_TIPG, 0x0060200A);
  265. }
  266. void E1000NetworkAdapter::out8(u16 address, u8 data)
  267. {
  268. #ifdef E1000_DEBUG
  269. dbg() << "E1000: OUT @ 0x" << address;
  270. #endif
  271. if (m_use_mmio) {
  272. auto* ptr = (volatile u8*)(m_mmio_base.get() + address);
  273. *ptr = data;
  274. return;
  275. }
  276. IO::out8(m_io_base + address, data);
  277. }
  278. void E1000NetworkAdapter::out16(u16 address, u16 data)
  279. {
  280. #ifdef E1000_DEBUG
  281. dbg() << "E1000: OUT @ 0x" << address;
  282. #endif
  283. if (m_use_mmio) {
  284. auto* ptr = (volatile u16*)(m_mmio_base.get() + address);
  285. *ptr = data;
  286. return;
  287. }
  288. IO::out16(m_io_base + address, data);
  289. }
  290. void E1000NetworkAdapter::out32(u16 address, u32 data)
  291. {
  292. #ifdef E1000_DEBUG
  293. dbg() << "E1000: OUT @ 0x" << address;
  294. #endif
  295. if (m_use_mmio) {
  296. auto* ptr = (volatile u32*)(m_mmio_base.get() + address);
  297. *ptr = data;
  298. return;
  299. }
  300. IO::out32(m_io_base + address, data);
  301. }
  302. u8 E1000NetworkAdapter::in8(u16 address)
  303. {
  304. #ifdef E1000_DEBUG
  305. dbg() << "E1000: IN @ 0x" << address;
  306. #endif
  307. if (m_use_mmio)
  308. return *(volatile u8*)(m_mmio_base.get() + address);
  309. return IO::in8(m_io_base + address);
  310. }
  311. u16 E1000NetworkAdapter::in16(u16 address)
  312. {
  313. #ifdef E1000_DEBUG
  314. dbg() << "E1000: IN @ 0x " << address;
  315. #endif
  316. if (m_use_mmio)
  317. return *(volatile u16*)(m_mmio_base.get() + address);
  318. return IO::in16(m_io_base + address);
  319. }
  320. u32 E1000NetworkAdapter::in32(u16 address)
  321. {
  322. #ifdef E1000_DEBUG
  323. dbg() << "E1000: IN @ 0x" << address;
  324. #endif
  325. if (m_use_mmio)
  326. return *(volatile u32*)(m_mmio_base.get() + address);
  327. return IO::in32(m_io_base + address);
  328. }
  329. void E1000NetworkAdapter::send_raw(const u8* data, size_t length)
  330. {
  331. disable_irq();
  332. u32 tx_current = in32(REG_TXDESCTAIL);
  333. #ifdef E1000_DEBUG
  334. kprintf("E1000: Sending packet (%zu bytes)\n", length);
  335. #endif
  336. auto& descriptor = m_tx_descriptors[tx_current];
  337. ASSERT(length <= 8192);
  338. auto* vptr = (void*)(descriptor.addr + 0xc0000000);
  339. memcpy(vptr, data, length);
  340. descriptor.length = length;
  341. descriptor.status = 0;
  342. descriptor.cmd = CMD_EOP | CMD_IFCS | CMD_RS;
  343. #ifdef E1000_DEBUG
  344. kprintf("E1000: Using tx descriptor %d (head is at %d)\n", tx_current, in32(REG_TXDESCHEAD));
  345. #endif
  346. tx_current = (tx_current + 1) % number_of_tx_descriptors;
  347. out32(REG_TXDESCTAIL, tx_current);
  348. cli();
  349. enable_irq();
  350. for (;;) {
  351. if (descriptor.status) {
  352. sti();
  353. break;
  354. }
  355. Thread::current->wait_on(m_wait_queue);
  356. }
  357. #ifdef E1000_DEBUG
  358. kprintf("E1000: Sent packet, status is now %b!\n", descriptor.status);
  359. #endif
  360. }
  361. void E1000NetworkAdapter::receive()
  362. {
  363. u32 rx_current;
  364. for (;;) {
  365. rx_current = in32(REG_RXDESCTAIL);
  366. if (rx_current == in32(REG_RXDESCHEAD))
  367. return;
  368. rx_current = (rx_current + 1) % number_of_rx_descriptors;
  369. if (!(m_rx_descriptors[rx_current].status & 1))
  370. break;
  371. auto* buffer = (u8*)(m_rx_descriptors[rx_current].addr + 0xc0000000);
  372. u16 length = m_rx_descriptors[rx_current].length;
  373. #ifdef E1000_DEBUG
  374. kprintf("E1000: Received 1 packet @ %p (%zu) bytes!\n", buffer, length);
  375. #endif
  376. did_receive(buffer, length);
  377. m_rx_descriptors[rx_current].status = 0;
  378. out32(REG_RXDESCTAIL, rx_current);
  379. }
  380. }
  381. }