E1000NetworkAdapter.cpp 17 KB

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