The TCP/IP stack reached straight into the first driver anyone wrote.
net.c called e1000_present(), the lwIP glue was named netif_e1000.c and
called e1000_transmit(), and include/defs.h declared the driver's
internals to the whole kernel. The second NIC driver would therefore
have arrived with a near-identical copy of the glue file and a patch to
the protocol stack, which is the arrangement include/device.h was
written to prevent -- the e1000 even said so, with `.ops = 0, // no
class vtable yet; there is no net class`.
There is now. A driver publishes a struct netdev_ops -- transmit,
receive, mac, link_up -- and is finished: it never mentions lwIP, and
nothing above it learns its name. The display class was already this
shape; this is the same shape for networking, and the two now read the
same way in kernel/dev/device.c.
What that made possible immediately:
- One netif per bound device instead of one per driver. lwIP's glue is
netif_eth.c, it takes a struct device, and it holds up to NETIF_MAX
interfaces. net.c attaches every network device it finds and starts
DHCP on each, because which interface the network answers on is not
ours to decide.
- /proc/netif lists all of them, one block of fields each, with the
interface name and the driver that claimed it.
- The ARP self-test moved from the e1000 to the class. Nothing in it
was chip-specific, and it is the first thing anyone will want to run
against the next driver. Still `echo n > /proc/sysrq`.
- Private headers live with their driver: e1000.h was in include/,
where exactly one file could use it. The driver build now adds the
driver's own directory to the include path.
The e1000 also drives more than one card. Its probe() refused the second
outright, because there was a single static instance -- so a machine
with two of them drove one and silently ignored the other. Instances are
an array now, and the instance is hung on dev->priv by a new optional
`attached` hook that the bus calls once the device exists. probe() runs
before device_add(), so there was previously nowhere to put it; that is
the whole reason the hook exists rather than a line inside probe.
QEMU gets an explicit NIC on every target (QEMU_NET). The default device
happens to be an e1000 on this machine type today, which is exactly the
sort of thing that stays true until a QEMU release changes it and the
network stops being exercised without anyone noticing.
Verified:
- bootcheck -Net passes, 48 [ok], no failures -- including the moved
self-test and two new checks that /proc/netif names both the
interface and the driver behind it, which together say the stack
went through the class rather than through a driver it was compiled
to know.
- Two NICs attached by hand (-device e1000 twice, second with its own
MAC): eth0 and eth1 both attach, each reports its own MAC in
/proc/netif, and each gets its own DHCP lease. Before this commit
the same machine produced one interface.
- make schfs-check still passes in full.
One trap worth recording: moving a header broke the build for anyone
with stale .d files -- make stopped with "No rule to make target
'include/e1000.h'". CFLAGS gains -MP, which emits a phony target per
header so a moved or deleted one can never wedge the build again.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
119 lines
4.8 KiB
C
119 lines
4.8 KiB
C
#pragma once
|
|
|
|
// PCI bus enumeration and driver binding.
|
|
//
|
|
// Until this existed, every driver in SchOS found its hardware by knowing
|
|
// where it was: IDE at 0x1F0 because IDE has always been at 0x1F0, the
|
|
// framebuffer wherever the BIOS's VBE call said. That works for exactly the
|
|
// devices that predate PCI and for nothing else. A NIC, an AHCI controller,
|
|
// a virtio device or a real GPU has no fixed address at all -- you find it by
|
|
// asking the bus what is plugged into it, which is what this does.
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Config space
|
|
// ---------------------------------------------------------------------------
|
|
|
|
#define PCI_CONFIG_ADDR 0xCF8
|
|
#define PCI_CONFIG_DATA 0xCFC
|
|
|
|
#define PCI_VENDOR_ID 0x00
|
|
#define PCI_DEVICE_ID 0x02
|
|
#define PCI_COMMAND 0x04
|
|
#define PCI_STATUS 0x06
|
|
#define PCI_REVISION 0x08
|
|
#define PCI_PROG_IF 0x09
|
|
#define PCI_SUBCLASS 0x0A
|
|
#define PCI_CLASS 0x0B
|
|
#define PCI_HEADER_TYPE 0x0E
|
|
#define PCI_BAR0 0x10
|
|
#define PCI_SECONDARY_BUS 0x19
|
|
#define PCI_INTERRUPT_LINE 0x3C
|
|
#define PCI_INTERRUPT_PIN 0x3D
|
|
|
|
// PCI_COMMAND bits a driver usually wants set before touching its device.
|
|
#define PCI_CMD_IO 0x0001
|
|
#define PCI_CMD_MEMORY 0x0002
|
|
#define PCI_CMD_BUS_MASTER 0x0004
|
|
|
|
// Base classes worth naming. The full list is long and mostly irrelevant here.
|
|
#define PCI_CLASS_STORAGE 0x01
|
|
#define PCI_CLASS_NETWORK 0x02
|
|
#define PCI_CLASS_DISPLAY 0x03
|
|
#define PCI_CLASS_MULTIMEDIA 0x04
|
|
#define PCI_CLASS_BRIDGE 0x06
|
|
#define PCI_CLASS_SERIAL_BUS 0x0C
|
|
|
|
#define PCI_STATUS_CAPLIST 0x0010 // device has a capability list
|
|
#define PCI_CAP_PTR 0x34 // offset of the first capability
|
|
|
|
#define PCI_CAP_MSI 0x05
|
|
#define PCI_CAP_MSIX 0x11
|
|
#define PCI_CAP_VENDOR 0x09 // virtio's configuration lives here
|
|
#define PCI_CAP_PCIE 0x10
|
|
|
|
#define PCI_ANY 0xFFFF // wildcard in a match table
|
|
|
|
#define NPCI 64 // devices we will remember
|
|
|
|
struct pci_bar {
|
|
uint64 base; // already masked; 0 means "no BAR here"
|
|
uint64 size; // determined by write-ones-and-read-back
|
|
int is_io; // I/O port range rather than memory
|
|
int is_64; // consumed the following BAR slot too
|
|
int prefetch;
|
|
};
|
|
|
|
struct pci_dev {
|
|
uint8 bus, slot, func;
|
|
uint16 vendor, device;
|
|
uint8 class, subclass, prog_if, revision;
|
|
uint8 irq; // interrupt line, 0xFF when unrouted
|
|
uint8 header_type;
|
|
struct pci_bar bar[6];
|
|
const char *driver; // name of whatever claimed it, or 0
|
|
};
|
|
|
|
// One row of a driver's match table. Terminate the table with a row whose
|
|
// vendor is 0.
|
|
struct pci_match {
|
|
uint16 vendor, device; // PCI_ANY to ignore
|
|
uint8 class, subclass; // used only when vendor is PCI_ANY
|
|
};
|
|
|
|
// Declared here rather than defs.h because both take a struct pci_dev, which
|
|
// only this header defines. See kernel/dev/device.c for the model they feed.
|
|
// device.h is included rather than forward-declaring `enum dev_class`, which
|
|
// is a compiler extension and warns.
|
|
#include "device.h"
|
|
|
|
enum dev_class pci_class_of(const struct pci_dev *);
|
|
void pci_describe(const struct pci_dev *, char *out, int cap);
|
|
|
|
struct pci_driver {
|
|
const char *name;
|
|
// Base for the device's name in /proc/devices, if the driver's own name
|
|
// makes a bad one. `e1000` plus an index reads as "e10000"; a NIC wants to
|
|
// be `eth0`. Optional -- null means use `name`.
|
|
const char *devname;
|
|
const struct pci_match *match;
|
|
// Class-specific vtable, published on the device when this driver claims
|
|
// it -- struct display_ops for a display, and so on. Optional; a driver
|
|
// with nothing to offer a consumer leaves it null.
|
|
//
|
|
// It lives on the driver rather than being set inside probe() because
|
|
// pci_bind() registers the device *after* probe returns, so a driver has
|
|
// nothing to attach it to at the point it would want to.
|
|
const void *ops;
|
|
// Return 0 to claim the device. A driver that returns non-zero leaves it
|
|
// unclaimed for anything else that matches.
|
|
int (*probe)(struct pci_dev *);
|
|
// Called right after the device is published, for a driver that supports
|
|
// more than one instance and needs to say which of its own is which.
|
|
// Optional -- a single-instance driver has nothing to attach.
|
|
//
|
|
// It exists because probe() runs *before* device_add(), so a driver has
|
|
// nowhere to put its per-instance pointer at the point it would want to.
|
|
// Without this the second card of the same model is either refused or
|
|
// silently shares the first one's registers.
|
|
void (*attached)(struct pci_dev *, struct device *);
|
|
};
|