aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / virtio_net.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* virtio_net.c -- virtio-net discovery (see virtio_net.h). Freestanding. */ |
| 18 | #include "virtio_net.h" |
| 19 | #include "ck.h" |
| 20 | |
| 21 | static uint8_t cfg_image[4096]; |
| 22 | |
| 23 | int ck_virtio_net_probe(const pci_system *pci, virtio_pci_caps *caps, const pci_func **func_out) |
| 24 | { |
| 25 | if (func_out) *func_out = 0; |
| 26 | const pci_func *f = pci_find_id(pci, 0x1af4u, 0x1041u); |
| 27 | if (!f) { |
| 28 | f = pci_find_id(pci, 0x1af4u, 0x1000u); /* transitional: subsystem 1 = net */ |
| 29 | if (f && f->subsys_id != 1u) f = 0; |
| 30 | } |
| 31 | if (!f) { |
| 32 | ck_printf("virtio_net: not bound (no virtio-net function)\n"); |
| 33 | return 0; |
| 34 | } |
| 35 | for (uint32_t off = 0; off < sizeof cfg_image; off += 4) { |
| 36 | uint32_t v = pci_r32(f->cfg, off); |
| 37 | cfg_image[off] = (uint8_t)v; |
| 38 | cfg_image[off + 1] = (uint8_t)(v >> 8); |
| 39 | cfg_image[off + 2] = (uint8_t)(v >> 16); |
| 40 | cfg_image[off + 3] = (uint8_t)(v >> 24); |
| 41 | } |
| 42 | virtio_pci_err e = virtio_pci_parse_caps(cfg_image, sizeof cfg_image, caps); |
| 43 | if (e != VIRTIO_PCI_OK) { |
| 44 | ck_printf("virtio_net: %02x:%02x.%u caps refused (%s)\n", f->bus, f->dev, f->fn, virtio_pci_err_name(e)); |
| 45 | ck_printf("virtio_net: not bound (capability parse failed)\n"); |
| 46 | return (int)e; |
| 47 | } |
| 48 | ck_printf("virtio_net: %02x:%02x.%u caps ok common=bar%u+0x%x notify=bar%u+0x%x mult=%u isr=bar%u+0x%x device=bar%u+0x%x\n", |
| 49 | f->bus, f->dev, f->fn, caps->common_cfg.bar, caps->common_cfg.offset, caps->notify_cfg.bar, |
| 50 | caps->notify_cfg.offset, caps->notify_off_multiplier, caps->isr_cfg.bar, caps->isr_cfg.offset, |
| 51 | caps->device_cfg.bar, caps->device_cfg.offset); |
| 52 | if (func_out) *func_out = f; |
| 53 | return 0; |
| 54 | } |