E1000NetworkAdapter.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/OwnPtr.h>
  8. #include <Kernel/Bus/PCI/Access.h>
  9. #include <Kernel/Bus/PCI/Device.h>
  10. #include <Kernel/IO.h>
  11. #include <Kernel/Interrupts/IRQHandler.h>
  12. #include <Kernel/Net/NetworkAdapter.h>
  13. #include <Kernel/Random.h>
  14. namespace Kernel {
  15. class E1000NetworkAdapter : public NetworkAdapter
  16. , public PCI::Device
  17. , public IRQHandler {
  18. public:
  19. static RefPtr<E1000NetworkAdapter> try_to_initialize(PCI::Address);
  20. virtual bool initialize();
  21. virtual ~E1000NetworkAdapter() override;
  22. virtual void send_raw(ReadonlyBytes) override;
  23. virtual bool link_up() override;
  24. virtual i32 link_speed() override;
  25. virtual bool link_full_duplex() override;
  26. virtual StringView purpose() const override { return class_name(); }
  27. protected:
  28. void setup_interrupts();
  29. void setup_link();
  30. E1000NetworkAdapter(PCI::Address, u8 irq);
  31. virtual bool handle_irq(const RegisterState&) override;
  32. virtual StringView class_name() const override { return "E1000NetworkAdapter"sv; }
  33. struct [[gnu::packed]] e1000_rx_desc {
  34. volatile uint64_t addr { 0 };
  35. volatile uint16_t length { 0 };
  36. volatile uint16_t checksum { 0 };
  37. volatile uint8_t status { 0 };
  38. volatile uint8_t errors { 0 };
  39. volatile uint16_t special { 0 };
  40. };
  41. struct [[gnu::packed]] e1000_tx_desc {
  42. volatile uint64_t addr { 0 };
  43. volatile uint16_t length { 0 };
  44. volatile uint8_t cso { 0 };
  45. volatile uint8_t cmd { 0 };
  46. volatile uint8_t status { 0 };
  47. volatile uint8_t css { 0 };
  48. volatile uint16_t special { 0 };
  49. };
  50. virtual void detect_eeprom();
  51. virtual u32 read_eeprom(u8 address);
  52. void read_mac_address();
  53. void write_command(u16 address, u32);
  54. u32 read_command(u16 address);
  55. void initialize_rx_descriptors();
  56. void initialize_tx_descriptors();
  57. void out8(u16 address, u8);
  58. void out16(u16 address, u16);
  59. void out32(u16 address, u32);
  60. u8 in8(u16 address);
  61. u16 in16(u16 address);
  62. u32 in32(u16 address);
  63. void receive();
  64. static constexpr size_t number_of_rx_descriptors = 32;
  65. static constexpr size_t number_of_tx_descriptors = 8;
  66. IOAddress m_io_base;
  67. VirtualAddress m_mmio_base;
  68. OwnPtr<Memory::Region> m_rx_descriptors_region;
  69. OwnPtr<Memory::Region> m_tx_descriptors_region;
  70. OwnPtr<Memory::Region> m_rx_buffer_region;
  71. OwnPtr<Memory::Region> m_tx_buffer_region;
  72. Array<void*, number_of_rx_descriptors> m_rx_buffers;
  73. Array<void*, number_of_tx_descriptors> m_tx_buffers;
  74. OwnPtr<Memory::Region> m_mmio_region;
  75. u8 m_interrupt_line { 0 };
  76. bool m_has_eeprom { false };
  77. bool m_use_mmio { false };
  78. EntropySource m_entropy_source;
  79. WaitQueue m_wait_queue;
  80. };
  81. }