Serenity Operating System
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
27#pragma once
28
29#include <AK/OwnPtr.h>
30#include <Kernel/Interrupts/IRQHandler.h>
31#include <Kernel/Net/NetworkAdapter.h>
32#include <Kernel/PCI/Access.h>
33#include <Kernel/PCI/Device.h>
34#include <LibBareMetal/IO.h>
35
36namespace Kernel {
37
38class E1000NetworkAdapter final : public NetworkAdapter
39 , public PCI::Device {
40public:
41 static void detect(const PCI::Address&);
42
43 E1000NetworkAdapter(PCI::Address, u8 irq);
44 virtual ~E1000NetworkAdapter() override;
45
46 virtual void send_raw(const u8*, size_t) override;
47 virtual bool link_up() override;
48
49 virtual const char* purpose() const override { return class_name(); }
50
51private:
52 virtual void handle_irq(const RegisterState&) override;
53 virtual const char* class_name() const override { return "E1000NetworkAdapter"; }
54
55 struct [[gnu::packed]] e1000_rx_desc
56 {
57 volatile uint64_t addr { 0 };
58 volatile uint16_t length { 0 };
59 volatile uint16_t checksum { 0 };
60 volatile uint8_t status { 0 };
61 volatile uint8_t errors { 0 };
62 volatile uint16_t special { 0 };
63 };
64
65 struct [[gnu::packed]] e1000_tx_desc
66 {
67 volatile uint64_t addr { 0 };
68 volatile uint16_t length { 0 };
69 volatile uint8_t cso { 0 };
70 volatile uint8_t cmd { 0 };
71 volatile uint8_t status { 0 };
72 volatile uint8_t css { 0 };
73 volatile uint16_t special { 0 };
74 };
75
76 void detect_eeprom();
77 u32 read_eeprom(u8 address);
78 void read_mac_address();
79
80 void write_command(u16 address, u32);
81 u32 read_command(u16 address);
82
83 void initialize_rx_descriptors();
84 void initialize_tx_descriptors();
85
86 void out8(u16 address, u8);
87 void out16(u16 address, u16);
88 void out32(u16 address, u32);
89 u8 in8(u16 address);
90 u16 in16(u16 address);
91 u32 in32(u16 address);
92
93 void receive();
94
95 IOAddress m_io_base;
96 VirtualAddress m_mmio_base;
97 OwnPtr<Region> m_rx_descriptors_region;
98 OwnPtr<Region> m_tx_descriptors_region;
99 Vector<OwnPtr<Region>> m_rx_buffers_regions;
100 Vector<OwnPtr<Region>> m_tx_buffers_regions;
101 OwnPtr<Region> m_mmio_region;
102 u8 m_interrupt_line { 0 };
103 bool m_has_eeprom { false };
104 bool m_use_mmio { false };
105
106 static const int number_of_rx_descriptors = 32;
107 static const int number_of_tx_descriptors = 8;
108
109 WaitQueue m_wait_queue;
110};
111}