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