Skip to content

Commit d121d73

Browse files
committed
solo5: Remove references to PCI, fix issues with device registration
1 parent 4e72a1e commit d121d73

7 files changed

Lines changed: 51 additions & 147 deletions

File tree

api/kernel/solo5_manager.hpp

Lines changed: 5 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -18,109 +18,19 @@
1818
#ifndef KERNEL_SOLO5_MANAGER_HPP
1919
#define KERNEL_SOLO5_MANAGER_HPP
2020

21-
#include <vector>
22-
#include <cstdio>
23-
#include <unordered_map>
21+
#include <memory>
2422
#include <delegate>
25-
26-
#include <hw/pci_device.hpp>
2723
#include <hw/devices.hpp>
2824

2925
class Solo5_manager {
30-
private:
31-
using Device_registry = std::unordered_map<PCI::classcode, std::vector<hw::PCI_Device>>;
32-
3326
public:
34-
template <PCI::classcode CLASS>
35-
static hw::PCI_Device& device(const int n) noexcept {
36-
return devices_[CLASS][n];
37-
};
38-
39-
template <PCI::classcode CLASS>
40-
static size_t num_of_devices() noexcept {
41-
return devices_[CLASS].size();
42-
}
43-
44-
/** Whats being stored and returned is a unique_ptr of the given device */
45-
template <typename Device_type>
46-
using Dev_ptr = std::unique_ptr<Device_type>;
47-
48-
/** The function (factory) thats create the Dev_ptr from PCI_Device, supplied by the driver */
49-
template <typename Device_type>
50-
using Driver_factory = delegate< Dev_ptr<Device_type>(hw::PCI_Device&) >;
51-
52-
/**
53-
* @brief Register a specific type of driver factory
54-
* @details Register a specific type of driver factory
55-
* (function that creates a pointer to the given Device, with its full driver implementation)
56-
* Indexed as a combination of vendor + product
57-
*
58-
* @param vendor Driver vendor id
59-
* @param product Driver product id
60-
* @param driver_factory Function for creating a driver
61-
* @tparam Device_type The specific type of Device the driver is for
62-
*/
63-
template <typename Device_type>
64-
static void register_driver(uint16_t vendor, uint16_t product,
65-
Driver_factory<Device_type> driver_factory)
66-
{
67-
drivers<Device_type>().emplace(get_driver_id(vendor, product), driver_factory);
68-
}
69-
70-
/** Currently a combination of Model + Product (we don't care about the revision etc. atm.)*/
71-
using driver_id_t = uint32_t;
72-
73-
/** Combine vendor and product id to represent a driver id */
74-
static driver_id_t get_driver_id(uint16_t vendor, uint16_t product)
75-
{ return (uint32_t)(vendor) << 16 | product; }
27+
using Nic_ptr = std::unique_ptr<hw::Nic>;
28+
using Blk_ptr = std::unique_ptr<hw::Block_device>;
7629

77-
static driver_id_t get_driver_id(hw::PCI_Device& dev)
78-
{ return get_driver_id(dev.vendor_id(), dev.product_id()); }
30+
static void register_net(delegate<Nic_ptr()>);
31+
static void register_blk(delegate<Blk_ptr()>);
7932

80-
private:
81-
static Device_registry devices_;
82-
83-
/** A register for a specific type of drivers: map[driver_id, driver_factory]*/
84-
template <typename Device_type>
85-
using Driver_registry = std::unordered_map<driver_id_t, Driver_factory<Device_type> >;
86-
87-
/**
88-
* @brief Retrieve drivers (factories) of a given type of device
89-
*
90-
* @return A collection of driver factories indexed by driver_id
91-
*/
92-
template <typename Device_type>
93-
static Driver_registry<Device_type>& drivers() {
94-
static Driver_registry<Device_type> drivers_;
95-
return drivers_;
96-
}
97-
98-
template <typename Device_type>
99-
inline static bool register_device(hw::PCI_Device& dev);
100-
101-
/**
102-
* Keep track of certain devices
103-
*
104-
* The PCI manager can probe and keep track of devices which can (possibly)
105-
* be specialized by the Dev-class later.
106-
*/
10733
static void init();
108-
109-
friend class OS;
11034
}; //< class Solo5_manager
11135

112-
template <typename Device_type>
113-
inline bool Solo5_manager::register_device(hw::PCI_Device& dev) {
114-
try {
115-
auto driver_factory = drivers<Device_type>().at(get_driver_id(dev));
116-
INFO2("| +--+ Driver: Found");
117-
118-
hw::Devices::register_device(driver_factory(dev));
119-
return true;
120-
} catch(std::out_of_range) {
121-
INFO2("| +--+ Driver: Not found");
122-
}
123-
return false;
124-
}
125-
12636
#endif //< KERNEL_SOLO5_MANAGER_HPP

src/drivers/solo5blk.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ extern "C" {
1313
#include <solo5.h>
1414
}
1515

16-
Solo5Blk::Solo5Blk(hw::PCI_Device& d)
16+
Solo5Blk::Solo5Blk()
1717
: hw::Block_device()
1818
{
1919
INFO("Solo5Blk", "Block device with %llu sectors", solo5_blk_sectors());
@@ -48,7 +48,6 @@ void Solo5Blk::deactivate()
4848

4949
struct Autoreg_solo5blk {
5050
Autoreg_solo5blk() {
51-
Solo5_manager::register_driver<hw::Block_device>(PCI::VENDOR_SOLO5, 0x1001,
52-
&Solo5Blk::new_instance);
51+
Solo5_manager::register_blk(&Solo5Blk::new_instance);
5352
}
5453
} autoreg_solo5blk;

src/drivers/solo5blk.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class Solo5Blk : public hw::Block_device
2929
{
3030
public:
3131

32-
static std::unique_ptr<Block_device> new_instance(hw::PCI_Device& d)
32+
static std::unique_ptr<Block_device> new_instance()
3333
{
34-
return std::make_unique<Solo5Blk>(d);
34+
return std::make_unique<Solo5Blk>();
3535
}
3636

3737
static constexpr size_t SECTOR_SIZE = 512;
@@ -68,7 +68,7 @@ class Solo5Blk : public hw::Block_device
6868
void deactivate() override; // stays
6969

7070
/** Constructor. @param pcidev an initialized PCI device. */
71-
Solo5Blk(hw::PCI_Device& pcidev);
71+
Solo5Blk();
7272

7373
private:
7474
// stat counters

src/drivers/solo5net.cpp

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,31 @@
2121

2222
#include "solo5net.hpp"
2323
#include <net/packet.hpp>
24-
#include <kernel/irq_manager.hpp>
25-
#include <kernel/syscalls.hpp>
2624
#include <hw/pci.hpp>
27-
#include <stdio.h>
28-
#include <malloc.h>
29-
#include <string.h>
25+
#include <cstdio>
26+
#include <cstring>
3027

3128
extern "C" {
3229
#include <solo5.h>
3330
}
3431

3532
using namespace net;
3633

37-
constexpr size_t MTU_ = 1520;
38-
constexpr size_t BUFFER_CNT = 1000;
39-
40-
// XXX: do we really need a bufstore?
41-
BufferStore solo5_bufstore{ BUFFER_CNT, MTU_ };
42-
4334
const char* Solo5Net::driver_name() const { return "Solo5Net"; }
4435

45-
Solo5Net::Solo5Net(hw::PCI_Device& d)
36+
Solo5Net::Solo5Net()
4637
: Link(Link_protocol{{this, &Solo5Net::transmit}, mac()},
4738
2048u, sizeof(net::Packet) + MTU()),
4839
packets_rx_{Statman::get().create(Stat::UINT64, device_name() + ".packets_rx").get_uint64()},
4940
packets_tx_{Statman::get().create(Stat::UINT64, device_name() + ".packets_tx").get_uint64()}
5041
{
5142
INFO("Solo5Net", "Driver initializing");
43+
mac_addr = MAC::Addr(solo5_net_mac_str());
5244
}
5345

5446
#include <cstdlib>
55-
void Solo5Net::transmit(net::Packet_ptr pckt) {
47+
void Solo5Net::transmit(net::Packet_ptr pckt)
48+
{
5649
net::Packet_ptr tail = std::move(pckt);
5750

5851
// Transmit all we can directly
@@ -64,7 +57,6 @@ void Solo5Net::transmit(net::Packet_ptr pckt) {
6457
net::Packet* pckt = tail.release();
6558
uint8_t *buf = pckt->buf();
6659

67-
printf("Solo5 writing packet %i bytes \n", pckt->size());
6860
solo5_net_write_sync(buf, pckt->size());
6961

7062
tail = std::move(next);
@@ -81,33 +73,29 @@ void Solo5Net::transmit(net::Packet_ptr pckt) {
8173
std::unique_ptr<Packet>
8274
Solo5Net::create_packet(int link_offset)
8375
{
84-
auto buffer = solo5_bufstore.get_buffer();
76+
auto buffer = bufstore().get_buffer();
8577
auto* pckt = (net::Packet*) buffer.addr;
8678

87-
new (pckt) net::Packet(link_offset, 0, MTU_, &solo5_bufstore);
79+
new (pckt) net::Packet(link_offset, 0, packet_len(), buffer.bufstore);
8880
return net::Packet_ptr(pckt);
8981
}
9082

9183
std::unique_ptr<Packet>
9284
Solo5Net::recv_packet()
9385
{
94-
auto buffer = solo5_bufstore.get_buffer();
86+
auto buffer = bufstore().get_buffer();
9587
auto* pckt = (net::Packet*) buffer.addr;
96-
int size = MTU_;
97-
new (pckt) net::Packet(0, size, MTU_, &solo5_bufstore);
88+
new (pckt) net::Packet(0, MTU(), packet_len(), &bufstore());
9889
uint8_t *buf = pckt->buf();
99-
memset(buf, 0, size);
10090
// Populate the packet buffer with new packet, if any
91+
int size = MTU();
10192
if (solo5_net_read_sync(buf, &size) == 0) {
10293
// Adjust packet size to match received data
103-
//printf("Solo5 data size: %i \n", size);
10494
if (size) {
10595
pckt->set_data_end(size);
106-
//printf("Solo5 Packet size: %i \n", pckt->size());
10796
return net::Packet_ptr(pckt);
10897
}
10998
}
110-
printf("Solo5 didn't get data. Size: %i \n", size);
11199
return nullptr;
112100
}
113101

@@ -127,7 +115,6 @@ void Solo5Net::deactivate()
127115

128116
struct Autoreg_solo5net {
129117
Autoreg_solo5net() {
130-
Solo5_manager::register_driver<hw::Nic>(PCI::VENDOR_SOLO5, 0x1000,
131-
&Solo5Net::new_instance);
118+
Solo5_manager::register_net(&Solo5Net::new_instance);
132119
}
133120
} autoreg_solo5net;

src/drivers/solo5net.hpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,34 @@ class Solo5Net : public net::Link_layer<net::Ethernet> {
3636
using Link = net::Link_layer<net::Ethernet>;
3737
using Link_protocol = Link::Protocol;
3838

39-
static std::unique_ptr<Nic> new_instance(hw::PCI_Device& d)
39+
static std::unique_ptr<Nic> new_instance()
4040
{
41-
return std::make_unique<Solo5Net>(d);
41+
return std::make_unique<Solo5Net>();
4242
}
4343

4444
/** Human readable name. */
4545
const char* driver_name() const override;
4646

4747
/** Mac address. */
48-
const MAC::Addr& mac() const noexcept override
49-
{
50-
return MAC::Addr(solo5_net_mac_str());
48+
const MAC::Addr& mac() const noexcept override {
49+
return mac_addr;
5150
}
5251

5352
uint16_t MTU() const noexcept override
5453
{ return 1500; }
5554

55+
uint16_t packet_len() const noexcept {
56+
return sizeof(net::ethernet::Header) + MTU();
57+
}
58+
5659
net::downstream create_physical_downstream()
5760
{ return {this, &Solo5Net::transmit}; }
5861

5962
/** Linklayer input. Hooks into IP-stack bottom, w.DOWNSTREAM data.*/
6063
void transmit(net::Packet_ptr pckt);
6164

6265
/** Constructor. @param pcidev an initialized PCI device. */
63-
Solo5Net(hw::PCI_Device& pcidev);
66+
Solo5Net();
6467

6568
/** Space available in the transmit queue, in packets */
6669
size_t transmit_queue_available() override {
@@ -83,7 +86,7 @@ class Solo5Net : public net::Link_layer<net::Ethernet> {
8386
void poll() override;
8487

8588
private:
86-
89+
MAC::Addr mac_addr;
8790
std::unique_ptr<net::Packet> recv_packet();
8891
/** Stats */
8992
uint64_t& packets_rx_;

src/kernel/solo5_manager.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,31 @@
2020
#include <delegate>
2121

2222
#include <kernel/solo5_manager.hpp>
23-
#include <hw/devices.hpp>
24-
#include <hw/pci_device.hpp>
2523
#include <stdexcept>
2624
#include <util/fixedvec.hpp>
2725

28-
static const int ELEMENTS = 16;
26+
static const int ELEMENTS = 4;
27+
using namespace hw;
28+
using Nic_ptr = std::unique_ptr<hw::Nic>;
29+
using Blk_ptr = std::unique_ptr<hw::Block_device>;
2930

30-
// PCI devices
31-
fixedvector<hw::PCI_Device, ELEMENTS> devices(Fixedvector_Init::UNINIT);
31+
fixedvector<delegate<Nic_ptr()>, ELEMENTS> nics(Fixedvector_Init::UNINIT);
32+
fixedvector<delegate<Blk_ptr()>, ELEMENTS> blks(Fixedvector_Init::UNINIT);
33+
34+
void Solo5_manager::register_net(delegate<Nic_ptr()> func)
35+
{
36+
nics.add(func);
37+
}
38+
void Solo5_manager::register_blk(delegate<Blk_ptr()> func)
39+
{
40+
blks.add(func);
41+
}
3242

3343
void Solo5_manager::init() {
3444
INFO("Solo5", "Looking for solo5 devices");
3545

36-
uint32_t id_net = 0x1000 << 16 | PCI::VENDOR_SOLO5;
37-
uint32_t id_blk = 0x1001 << 16 | PCI::VENDOR_SOLO5;
38-
39-
auto& stored_blk = devices.emplace(PCI::SOLO5_BLK_DUMMY_ADDR, id_blk, 0);
40-
auto& stored_net = devices.emplace(PCI::SOLO5_NET_DUMMY_ADDR, id_net, 0);
41-
42-
register_device<hw::Block_device>(stored_blk);
43-
register_device<hw::Nic>(stored_net);
46+
for (auto nic : nics)
47+
hw::Devices::register_device<hw::Nic> (nic());
48+
for (auto blk : blks)
49+
hw::Devices::register_device<hw::Block_device> (blk());
4450
}

test_ukvm.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
. ./etc/set_traps.sh
33

44
pushd examples/demo_service
5-
rm -rf build
65
mkdir -p build
76
pushd build
87
PLATFORM=x86_solo5 cmake ..

0 commit comments

Comments
 (0)