E1000NetworkAdapter.cpp 15 KB

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