E1000NetworkAdapter.cpp 15 KB

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